从模块关闭UserForm [英] Close UserForm from Module
问题描述
我正在尝试关闭一个模块中的用户表单,但是它不起作用。 b $ b
Sub UpdateSheetButton()
Dim subStr1 As String
Dim subSrrt2()As String
Dim tmp As Integer
Dim pos As Integer
Dim Form As WaitMessage
Set Form = New WaitMessage
使用窗体
.Message_wait = Module2.Label_PleaseWait
.Show
结束
对于ActiveSheet.UsedRange.Cells中的每个单元
subStr1 = RemoveTextBetween(Cell.formula,'C:\,\AddIns\XL -EZ Addin.xla'!)
tmp = Len(subStr1)< 1
如果tmp> = 0然后
Cell.formula = subStr1
status = True
结束If
下一个
卸载Form
MsgBox Module2.Label_ProcessComplete
End Sub
表单名称是 WaitMessage
。
我也尝试过 WaitMessage.Hide
但它也不起作用。
另一种可能是把你的代码放在 ClassModule 并使用事件回调WaitMessage用户窗体。这里简单的例子。 HTH
标准模块创建表单和updater对象,并显示开始处理的表单:
Public Sub Main()
/ pre>
Dim myUpdater As Updater
Dim myRange As Range
Dim myWaitMessage As WaitMessage
设置myRange = ActiveSheet.UsedRange.Cells
设置myUpdater =新更新程序
设置myUpdater.SourceRange = myRange
'创建并初始化窗体
设置myWaitMessage =新WaitMessage
使用myWaitMessage
.Caption =等待消息
设置.UpdaterObject = myUpdater
'...等
。显示
结束
MsgBoxModule2.Label_ProcessComplete
End Sub
类模块包含受监视的方法,并且如果进度更新或完成,则会生成事件。如果一些信息发送到表单,这里是已处理单元格的数量,但它可以是其他任何东西:
公共事件更新(updatedCellsCount As Long)
公共事件完成()
公共取消过程作为布尔
公共源范围作为范围
公开Sub UpdateSheetButton()
Dim subStr1 As String
Dim subSrrt2()As String
Dim tmp As Integer
Dim pos As Integer
Dim changesCount As Long
Dim myCell As Range
Dim Status
'进程任务,并通过事件回调表单并更新它
每个myCell在SourceRange.Cells
'检查CancelProcess变量,它由表单cancel-process按钮设置
如果CancelProcess然后_
退出对于
subStr1 =RemoveTextBetween(Cell.Formula,''C :\,\AddIns\XL-EZ Addin.xla'!)
tmp = Len(subStr1 ) 1
如果tmp> = 0然后
myCell.Formula = subStr1
Status = True
End If
changesCount = changesCount + 1
RaiseEvent更新(changesCount)
DoEvents
下一个
RaiseEvent已完成
End Sub
用户表单具有用WithEvent关键字声明的updater类的实例,并处理它的事件。此处更新已更新事件上的标签,并在完成事件上卸载自己:
Public WithEvents UpdaterObject As Updater
Private Sub UpdaterObject_Finished()
卸载Me
End Sub
Private Sub UpdaterObject_Updated(updatedCellsCount As Long)
progressLabel.Caption = updatedCellsCount
End Sub
Private Sub UserForm_Activate()
UpdaterObject.UpdateSheetButton
End Sub
Private Sub cancelButton_Click )
UpdaterObject.CancelProcess = True
End Sub
I am trying to Close a User Form from a module, but it's not working.
Here is what I have tried
Sub UpdateSheetButton() Dim subStr1 As String Dim subSrrt2() As String Dim tmp As Integer Dim pos As Integer Dim Form As WaitMessage Set Form = New WaitMessage With Form .Message_wait = Module2.Label_PleaseWait .Show End With For Each Cell In ActiveSheet.UsedRange.Cells subStr1 = RemoveTextBetween(Cell.formula, "'C:\", "\AddIns\XL-EZ Addin.xla'!") tmp = Len(subStr1) < 1 If tmp >= 0 Then Cell.formula = subStr1 status = True End If Next Unload Form MsgBox Module2.Label_ProcessComplete End Sub
Form Name is
WaitMessage
.I have also tried
WaitMessage.Hide
but it's also not working.解决方案Another possibility could be to put your code to ClassModule and to use Events to callback to WaitMessage user form. Here short example. HTH
Standard module creates the form and the updater object and displays the form which starts processing:
Public Sub Main() Dim myUpdater As Updater Dim myRange As Range Dim myWaitMessage As WaitMessage Set myRange = ActiveSheet.UsedRange.Cells Set myUpdater = New Updater Set myUpdater.SourceRange = myRange ' create and initialize the form Set myWaitMessage = New WaitMessage With myWaitMessage .Caption = "Wait message" Set .UpdaterObject = myUpdater ' ... etc. .Show End With MsgBox "Module2.Label_ProcessComplete" End Sub
Class module containes the monitored method and has events which are raised if progress updated or finished. In the event some information is send to the form, here it is the number of processed cells but it can be anything else:
Public Event Updated(updatedCellsCount As Long) Public Event Finished() Public CancelProcess As Boolean Public SourceRange As Range Public Sub UpdateSheetButton() Dim subStr1 As String Dim subSrrt2() As String Dim tmp As Integer Dim pos As Integer Dim changesCount As Long Dim myCell As Range Dim Status ' process task and call back to form via event and update it For Each myCell In SourceRange.Cells ' check CancelProcess variable which is set by the form cancel-process button If CancelProcess Then _ Exit For subStr1 = "" ' RemoveTextBetween(Cell.Formula, "'C:\", "\AddIns\XL-EZ Addin.xla'!") tmp = Len(subStr1) < 1 If tmp >= 0 Then myCell.Formula = subStr1 Status = True End If changesCount = changesCount + 1 RaiseEvent Updated(changesCount) DoEvents Next RaiseEvent Finished End Sub
User form has instance of updater class declared with 'WithEvent' keyword and handles events of it. Here form updates a label on 'Updated' event and unloads itself on 'Finished' event:
Public WithEvents UpdaterObject As Updater Private Sub UpdaterObject_Finished() Unload Me End Sub Private Sub UpdaterObject_Updated(updatedCellsCount As Long) progressLabel.Caption = updatedCellsCount End Sub Private Sub UserForm_Activate() UpdaterObject.UpdateSheetButton End Sub Private Sub cancelButton_Click() UpdaterObject.CancelProcess = True End Sub
这篇关于从模块关闭UserForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!