Vba 用户窗体初始化检查,然后关闭
我有一个用户表单。其目的是检查“管理”表的第(15)列中是否有任何“真”值。如果至少有一个“True”值,则userform将保持打开状态并继续其操作 但是,如果没有找到一个“True”,则userform将显示一条消息并自动关闭userformVba 用户窗体初始化检查,然后关闭,vba,excel,userform,Vba,Excel,Userform,我有一个用户表单。其目的是检查“管理”表的第(15)列中是否有任何“真”值。如果至少有一个“True”值,则userform将保持打开状态并继续其操作 但是,如果没有找到一个“True”,则userform将显示一条消息并自动关闭userform Private Sub Userform_initialize() Dim LR As Long LR = Sheets("Project_Name").Cells(Rows.Count, "B").End(xlUp).Row
Private Sub Userform_initialize()
Dim LR As Long
LR = Sheets("Project_Name").Cells(Rows.Count, "B").End(xlUp).Row
With Worksheets("Admin")
For i = 7 To LR
If .Cells(i, 15) = "True" Then
Exit For
Else
MsgBox ("No values found")
Exit For
Unload Me
End If
Next i
End With
''' more code'''
End Sub
我的userform上的一切都按预期工作,除了我无法使它自动关闭之外。也就是说,卸载我不工作
有什么建议吗?请查看您的代码;你把我放在出口后
'Here is something for you to ponder on .........
'Public enum type to add a set of particular vbKeys to the standard key set
Public Enum typePressKeys
vbNoKey = 0
vbExitTrigger = -1
vbAnswerKey = 100
vbLaunchKey = 102
vbPrevious = 104
vbNext = 106
vbSpecialAccessKey = 108
End Enum
Public Sub doSomethingWithMyUserform()
Dim stopLoop As Boolean, testVal As Boolean, rngX As Range, LR As Long
LR = ThisWorkbook.Sheets("Project_Name").Cells(Rows.Count, "B").End(xlUp).Row
Set rngX = ThisWorkbook.Worksheets("Admin")
testVal = False
With rngX 'Your sub can do the check here
For i = 7 To LR
If .Cells(i, 15) = "True" Then
testVal = True
Exit For
End If
Next i
End With
If testVal Then
Load UserForm1
With UserForm1
.Caption = "Something"
.Tag = vbNoKey
.button_OK.SetFocus 'Assuming you have a OK button on Userform1
End With
UserForm1.Show
stopLoop = False
Do
If UserForm1.Tag = vbCancel Then
'Do something perhaps
Unload UserForm1
stopLoop = True
ElseIf UserForm1.Tag = vbOK Then
'Do something specific
Unload UserForm1
stopLoop = True
Else
stopLoop = False
End If
Loop Until stopLoop = True
else
MsgBox "No values found"
End If
'Here you can close the way you want
Set rngX = Nothing
End Sub
enter code here
请看你的密码;你把我放在出口后
'Here is something for you to ponder on .........
'Public enum type to add a set of particular vbKeys to the standard key set
Public Enum typePressKeys
vbNoKey = 0
vbExitTrigger = -1
vbAnswerKey = 100
vbLaunchKey = 102
vbPrevious = 104
vbNext = 106
vbSpecialAccessKey = 108
End Enum
Public Sub doSomethingWithMyUserform()
Dim stopLoop As Boolean, testVal As Boolean, rngX As Range, LR As Long
LR = ThisWorkbook.Sheets("Project_Name").Cells(Rows.Count, "B").End(xlUp).Row
Set rngX = ThisWorkbook.Worksheets("Admin")
testVal = False
With rngX 'Your sub can do the check here
For i = 7 To LR
If .Cells(i, 15) = "True" Then
testVal = True
Exit For
End If
Next i
End With
If testVal Then
Load UserForm1
With UserForm1
.Caption = "Something"
.Tag = vbNoKey
.button_OK.SetFocus 'Assuming you have a OK button on Userform1
End With
UserForm1.Show
stopLoop = False
Do
If UserForm1.Tag = vbCancel Then
'Do something perhaps
Unload UserForm1
stopLoop = True
ElseIf UserForm1.Tag = vbOK Then
'Do something specific
Unload UserForm1
stopLoop = True
Else
stopLoop = False
End If
Loop Until stopLoop = True
else
MsgBox "No values found"
End If
'Here you can close the way you want
Set rngX = Nothing
End Sub
enter code here
在显示
UserForm
之前,您应该检查您的条件。无论您在哪里调用UserForm
,都可以将其添加为条件。无需打开表格,只要在您可以进行检查时立即关闭即可
在True
的第一个实例中,UserForm
将打开,并退出sub。如果循环完成(未找到True
值),sub将进入您的MsgBox
Sub OpenForm
With Worksheets("Admin")
For i = 7 To LR
If Cells(i,15) = "True" then
Userform.Show
Exit Sub
End If
Next i
End With
MsgBox "No Values Found"
End Sub
在显示
UserForm
之前,您应该检查您的条件。无论您在哪里调用UserForm
,都可以将其添加为条件。无需打开表格,只要在您可以进行检查时立即关闭即可
在True
的第一个实例中,UserForm
将打开,并退出sub。如果循环完成(未找到True
值),sub将进入您的MsgBox
Sub OpenForm
With Worksheets("Admin")
For i = 7 To LR
If Cells(i,15) = "True" then
Userform.Show
Exit Sub
End If
Next i
End With
MsgBox "No Values Found"
End Sub
如果我在退出之前卸载我,它将继续循环。。并显示消息n次。@OdaySalim点是,
Initialize
处理程序中的Unload Me
毫无意义。此外,Exit
语句之后的任何代码也不能运行。停止使用表单的默认实例(即,New
up您的表单,而不是执行UserForm1.Show
),您将永远不需要卸载我
。如果我在退出之前将卸载我,它将继续循环。。并显示消息n次。@OdaySalim点是,Initialize
处理程序中的Unload Me
毫无意义。此外,Exit
语句之后的任何代码也不能运行。停止使用表单的默认实例(即,New
up您的表单,而不是执行UserForm1.Show
),您将永远不需要卸载我
。啊-所以您不需要在初始化时执行此操作。。。为什么不呢?听起来您想检查一些值,以确定是否要保持用户窗体处于打开状态。我假设您的值是在表单打开时确定(设置)的,因此您可以选择任何一种方式。只是在之前检查更为合理,这样,如果您的条件不满足,表单就不必连续打开/关闭。啊-所以您不必在初始化时执行此操作。。。为什么不呢?听起来您想检查一些值,以确定是否要保持用户窗体处于打开状态。我假设您的值是在表单打开时确定(设置)的,因此您可以选择任何一种方式。只需在检查之前进行检查,这样在不满足条件的情况下表单就不必连续打开/关闭。请注意,当代码正确缩进和间隔时,就更容易理解发生了什么。我建议您使用Exit For
找到一个替代方法,并确保通过将Option Explicit
放在[每个]模块[始终]的顶部来声明所有变量。此外,在某些情况下,不要打开不需要显示的表单,而是在打开它之前先检查它是否需要显示。“1.喝杯水。2.我渴了吗?如果渴了,倒掉水。”:-)Initialize
在实例化类时运行。如果显示的是默认实例,它可能运行,也可能根本不运行,这取决于它上次关闭的方式<如果不使用默认实例,则根本不需要code>Unload Me。同样,卸载一个正在构建的对象,对我来说也不是一个好主意。最后,Exit
语句之后的任何代码都不能运行。你需要检查一下你的控制流,有些东西不对劲了。请注意,当代码适当地缩进和间隔时,理解发生了什么就容易多了。我建议您使用Exit For
找到一个替代方法,并确保通过将Option Explicit
放在[每个]模块[始终]的顶部来声明所有变量。此外,在某些情况下,不要打开不需要显示的表单,而是在打开它之前先检查它是否需要显示。“1.喝杯水。2.我渴了吗?如果渴了,倒掉水。”:-)Initialize
在实例化类时运行。如果显示的是默认实例,它可能运行,也可能根本不运行,这取决于它上次关闭的方式<如果不使用默认实例,则根本不需要code>Unload Me。同样,卸载一个正在构建的对象,对我来说也不是一个好主意。最后,Exit
语句之后的任何代码都不能运行。你需要检查一下你的控制流程,有点问题。