在不终止子例程的情况下隐藏用户窗体
Hiding UserForm Without Terminating Subroutine
我有一个用户窗体,用户可以从中选择在代码中进一步使用的活动工作表中进行选择。
Private Sub CommandButton1_Click()
Dim results As String
Me.Hide
results = get_range_from_selection()
Me.Show
TextBox.Text = results
End Sub
Public Function get_range_from_selection() As String
Dim selection As Range
Set selection = Application.InputBox("Select rows.", "Select Rows", Type:=8)
get_range_from_selection = selection.Address
End Function
但是,在单个屏幕上,用户窗体会妨碍工作表,因此很难进行选择,因此我需要以某种方式隐藏/最小化窗体。
我试过使用UserForm.Hide方法,但问题是虽然用户窗体保留了textboks值等,但它似乎终止了按钮_Click()事件子例程,该子例程调用了上面代码中的函数第一名。该函数执行到最后,但应该根据选择在用户窗体中执行操作的 _Click() 子例程在 UserForm.Show.
时终止
我试过将 .Hide/.Show 放在 _Click() 子例程和被调用函数中,但结果是一样的。它应该表现得像这样吗?有没有我可以尝试的解决方法或其他方法?
我建议您可以在您的用户表单中尝试以下代码。我假设该代码属于一个命令按钮,需要单击该按钮才能 select 范围。
Private Sub CommandButton1_Click()
Dim rg As Range
Me.Hide
Set rg = Application.InputBox("Select rows.", "Select Rows", Type:=8)
' Be careful as the user could select a range from a different sheet
' or even a different workbook.
If rg.Parent Is ActiveSheet Then
rg.Select
End If
Me.Show
End Sub
您可能使用了 Userform1.Hide
而不是 Me.Hide
之类的东西,这经常会导致问题,看看 here or here。
更新: 你也可以"hide"这样的表格。
Private Sub CommandButton1_Click()
Dim rg As Range
Dim fHeight As Double
Dim fWidth As Double
fHeight = Me.Height
fWidth = Me.Width
Me.Height = 0
Me.Width = 0
Set rg = Application.InputBox("Select rows.", "Select Rows", Type:=8)
' Be careful as the user could select a range from a different sheet
If rg.Parent Is ActiveSheet Then
rg.Select
End If
Me.Height = fHeight
Me.Width = fWidth
End Sub
在这种情况下,代码不会被 Me.Show
"stopped"。
更新 2:基于更新后的 post,您可以在用户表单中尝试以下代码
Option Explicit
Dim fHeight As Double
Dim fWidth As Double
Private Sub CommandButton1_Click()
Dim results As String
meHide
results = get_range_from_selection()
meShow
TextBox.Text = results
End Sub
Private Function meHide()
fHeight = Me.Height
fWidth = Me.Width
Me.Height = 0
Me.Width = 0
End Function
Private Function meShow()
Me.Height = fHeight
Me.Width = fWidth
End Function
Public Function get_range_from_selection() As String
Dim selection As Range
Set selection = Application.InputBox("Select rows.", "Select Rows", Type:=8)
get_range_from_selection = selection.Address
End Function
我有一个用户窗体,用户可以从中选择在代码中进一步使用的活动工作表中进行选择。
Private Sub CommandButton1_Click()
Dim results As String
Me.Hide
results = get_range_from_selection()
Me.Show
TextBox.Text = results
End Sub
Public Function get_range_from_selection() As String
Dim selection As Range
Set selection = Application.InputBox("Select rows.", "Select Rows", Type:=8)
get_range_from_selection = selection.Address
End Function
但是,在单个屏幕上,用户窗体会妨碍工作表,因此很难进行选择,因此我需要以某种方式隐藏/最小化窗体。
我试过使用UserForm.Hide方法,但问题是虽然用户窗体保留了textboks值等,但它似乎终止了按钮_Click()事件子例程,该子例程调用了上面代码中的函数第一名。该函数执行到最后,但应该根据选择在用户窗体中执行操作的 _Click() 子例程在 UserForm.Show.
时终止我试过将 .Hide/.Show 放在 _Click() 子例程和被调用函数中,但结果是一样的。它应该表现得像这样吗?有没有我可以尝试的解决方法或其他方法?
我建议您可以在您的用户表单中尝试以下代码。我假设该代码属于一个命令按钮,需要单击该按钮才能 select 范围。
Private Sub CommandButton1_Click()
Dim rg As Range
Me.Hide
Set rg = Application.InputBox("Select rows.", "Select Rows", Type:=8)
' Be careful as the user could select a range from a different sheet
' or even a different workbook.
If rg.Parent Is ActiveSheet Then
rg.Select
End If
Me.Show
End Sub
您可能使用了 Userform1.Hide
而不是 Me.Hide
之类的东西,这经常会导致问题,看看 here or here。
更新: 你也可以"hide"这样的表格。
Private Sub CommandButton1_Click()
Dim rg As Range
Dim fHeight As Double
Dim fWidth As Double
fHeight = Me.Height
fWidth = Me.Width
Me.Height = 0
Me.Width = 0
Set rg = Application.InputBox("Select rows.", "Select Rows", Type:=8)
' Be careful as the user could select a range from a different sheet
If rg.Parent Is ActiveSheet Then
rg.Select
End If
Me.Height = fHeight
Me.Width = fWidth
End Sub
在这种情况下,代码不会被 Me.Show
"stopped"。
更新 2:基于更新后的 post,您可以在用户表单中尝试以下代码
Option Explicit
Dim fHeight As Double
Dim fWidth As Double
Private Sub CommandButton1_Click()
Dim results As String
meHide
results = get_range_from_selection()
meShow
TextBox.Text = results
End Sub
Private Function meHide()
fHeight = Me.Height
fWidth = Me.Width
Me.Height = 0
Me.Width = 0
End Function
Private Function meShow()
Me.Height = fHeight
Me.Width = fWidth
End Function
Public Function get_range_from_selection() As String
Dim selection As Range
Set selection = Application.InputBox("Select rows.", "Select Rows", Type:=8)
get_range_from_selection = selection.Address
End Function