Excel VBA用户窗体以显示所选的工作表
我已经创建了一个用户表单,它有一个列表框(ListBox1),它列出了工作表名称,我可以双击该列表,它将带我到该工作表。我有一个后退按钮(CommandButton2),当我点击后退按钮时,它会将我带到上一张选中的工作表。 我想链接我的列表框和后退按钮,这样当我单击后退按钮时,我的列表框(ListBox1)应该突出显示后退按钮(CommandButton2)指向的工作表 请查看以下我的代码:Excel VBA用户窗体以显示所选的工作表,vba,excel,Vba,Excel,我已经创建了一个用户表单,它有一个列表框(ListBox1),它列出了工作表名称,我可以双击该列表,它将带我到该工作表。我有一个后退按钮(CommandButton2),当我点击后退按钮时,它会将我带到上一张选中的工作表。 我想链接我的列表框和后退按钮,这样当我单击后退按钮时,我的列表框(ListBox1)应该突出显示后退按钮(CommandButton2)指向的工作表 请查看以下我的代码: Private Sub UserForm_Initialize() Dim Sh As Workshe
Private Sub UserForm_Initialize()
Dim Sh As Worksheet
'for each loop the add visible sheets
For Each Sh In ActiveWorkbook.Sheets
'add sheets to the listbox
Me.ListBox1.AddItem Sh.Name
Next Sh
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'declare the variables
' modifed code for ListBox double-click event, store the sheet name before switching to the selected item
Dim i As Long
LastSelectedSht = ActiveSheet.Name ' <-- save the current active sheet before selecting a new one
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Worksheets(ListBox1.List(i)).Activate
End If
Next i
End Sub
Private Sub CommandButton2_Click()
Dim TmpSht As String
TmpSht = ActiveSheet.Name ' <-- save the current active sheet
' select the previous sheet (stored in LastSelectedSht)
If LastSelectedSht = "" Then
MsgBox "Error, no sheet stored , is it your first time running ? "
Else
Sheets(LastSelectedSht).Select
End If
LastSelectedSht = TmpSht ' <-- use the temp variable to store the latest active sheet
' reset the userform
Unload Me
frmNavigation.Show
End Sub
Private子用户表单_Initialize()
将Sh设置为工作表
'为每个循环添加可见的工作表
对于ActiveWorkbook.Sheets中的每个Sh
'将工作表添加到列表框
Me.ListBox1.AddItem Sh.名称
下一个Sh
端接头
私有子ListBox1_DblClick(ByVal Cancel作为MSForms.ReturnBoolean)
'声明变量
'修改列表框双击事件的代码,在切换到所选项目之前存储工作表名称
我想我会坚持多久
LastSelectedSht=ActiveSheet.Name'此子项将更改列表框中的选定项
Private Sub SetListBox(Lbx As MSForms.ListBox, _
Itm As String)
Dim i As Integer
With Lbx
For i = 0 To .ListCount - 1
.Selected(i) = Not CBool(StrComp(.List(i), Itm))
Next i
End With
End Sub
从您的过程调用它,该过程使用一行代码激活上一个工作表,如下所示
SetListBox ListBox1, TmpSht
确保TmpSht
在您呼叫时保留新激活工作表的名称,或传递该工作表的名称而不是TmpSht。此子项将更改列表框中的选定项目
Private Sub SetListBox(Lbx As MSForms.ListBox, _
Itm As String)
Dim i As Integer
With Lbx
For i = 0 To .ListCount - 1
.Selected(i) = Not CBool(StrComp(.List(i), Itm))
Next i
End With
End Sub
从您的过程调用它,该过程使用一行代码激活上一个工作表,如下所示
SetListBox ListBox1, TmpSht
确保TmpSht
在您呼叫时保留新激活的工作表的名称,或传递该工作表的名称而不是TmpSht。无需所有这些循环来查找所选项目,您可以简化如下操作:
Option Explicit
Dim LastSelectedSht As String '<--| use as UserForm scoped variable to store the name of "last" sheet selected
Private Sub UserForm_Initialize()
Dim Sh As Worksheet
With Me.ListBox1
'for each loop the add visible sheets
For Each Sh In ActiveWorkbook.Sheets
.AddItem Sh.Name 'add sheets names to the listbox
Next Sh
LastSelectedSht = ActiveSheet.Name ' <-- store the currently active sheet name as the "last" one
.Value = LastSelectedSht '<--| initialize listbox selection with the currently active sheet name
End With
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
' modifed code for ListBox double-click event, store the sheet name before switching to the selected item
LastSelectedSht = ActiveSheet.Name
Worksheets(ListBox1.Value).Activate '<--| activate the sheet whose name has been dblclicked
End Sub
Private Sub CommandButton2_Click()
Sheets(LastSelectedSht).Activate
Me.ListBox1.Value = LastSelectedSht
' reset the userform
Unload Me
frmNavigation.Show
End Sub
选项显式
将LastSelectedSht设置为字符串“无需所有这些循环来查找所选项目,您可以将事情简化如下:
Option Explicit
Dim LastSelectedSht As String '<--| use as UserForm scoped variable to store the name of "last" sheet selected
Private Sub UserForm_Initialize()
Dim Sh As Worksheet
With Me.ListBox1
'for each loop the add visible sheets
For Each Sh In ActiveWorkbook.Sheets
.AddItem Sh.Name 'add sheets names to the listbox
Next Sh
LastSelectedSht = ActiveSheet.Name ' <-- store the currently active sheet name as the "last" one
.Value = LastSelectedSht '<--| initialize listbox selection with the currently active sheet name
End With
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
' modifed code for ListBox double-click event, store the sheet name before switching to the selected item
LastSelectedSht = ActiveSheet.Name
Worksheets(ListBox1.Value).Activate '<--| activate the sheet whose name has been dblclicked
End Sub
Private Sub CommandButton2_Click()
Sheets(LastSelectedSht).Activate
Me.ListBox1.Value = LastSelectedSht
' reset the userform
Unload Me
frmNavigation.Show
End Sub
选项显式
DimeLasStestDeSt作为字符串“我是否应该粘贴子StestListBox在模块和空白按钮(CurdButn2)类型调用StestListBox中,请指导我不能理解的是,我应该将子StestListBox粘贴在模块和空白按钮中(CudidButn2)键入call SetListBox,因为我无法understand@astha:以上代码是用户表单代码窗格中所需的所有代码,用于请求/显示的功能。如果你需要帮助,就问我,这是我应该做的,并且足够有活力来处理任何添加的表单。谢谢@Wowdude。现在让我们等待阿斯塔的反馈@Asha:上面的代码是用户表单代码窗格中用于请求/显示功能所需的所有代码。如果你需要帮助,就问我,这是我应该做的,并且足够有活力来处理任何添加的表单。谢谢@Wowdude。现在让我们等待阿斯塔的反馈!