我有一个用户表单。这个想法是检查“管理”表中的第(15)列中是否有任何“True”值。如果至少有一个“True”值,则用户窗体将保持打开状态并继续其操作。
但是,如果没有找到任何“True”,则用户窗体将显示一条消息并自动关闭用户窗体。
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
我的用户表单上的所有内容都按预期工作,除了我无法使其自动关闭之外。 IE。 卸载我不起作用。
有什么建议吗?
在显示
UserForm
之前,您应该检查您的标准。无论您在哪里调用 UserForm
,都可以将其添加为条件。当您可以事先检查时,无需打开表格即可立即关闭它。
在
True
的第一个实例上,UserForm
将打开,并退出子进程。如果循环完成(未找到 True
值),子程序将继续执行您的 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
请查看您的代码;您已将“卸载我”放在“退出”之后
'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
为了避免显示用户表单,我尝试了几乎所有方法,并且以下方法工作正常,至少对我来说: 在我的宏中,我同时使用 UF_initialize 和 UF_activate。 如果我尝试在“UF_initialize”中“卸载我”,系统会显示我想避免的错误。为了实现这一目标,我正在使用“错误处理”
我使用了一个名为“gStopProgramExecution”的全局变量,通过错误处理将其设置为 TRUE 即:
Private Sub UserForm_Initialize()
gStopProgramExecution = False
.......your code......
Call anySub ' upon return check if the var has been set to TRUE
If gStopProgramExecution Then GoTo abruptTermination
.......your code......
Exit Sub
abruptTermination:
gStopProgramExecution = True
MsgBox gAbruptProgramExecution, vbCritical
' If an error has happened, infrom the user that program will close abruptly
' VBA jumps automatically to UF_activate sub
..........
End Sub
------------------------------
Private Sub UserForm_Activate()
On Error GoTo abruptTermination
' Check first if you are coming here due to an error and if so, GoTo "abruptTermination:"
If gStopProgramExecution Then GoTo abruptTermination
.......your code......
' exit procedure normally
Exit Sub
abruptTermination:
gStopProgramExecution = True
'
' unload here the User Form.
' No error is generated, and the Program closes without unwanted generation of further error messages, displayed automatically by the system
'
Unload Me
End Sub
' 我希望这个方法有帮助