在excel vba userform中创建backbutton以转到上一个活动工作表

在excel vba userform中创建backbutton以转到上一个活动工作表,vba,excel,Vba,Excel,我已经创建了一个userformfrmNavigation,它有一个ListBox1,它将列出工作簿中的所有工作表,我可以双击列表框中列出的任何工作表并转到该工作表 现在我有将近50个工作表,所以我从ListBox1中出现的列表中双击并转到该工作表,但现在我需要一个返回按钮“CommandButton2”,这样它可以将我带回以前的活动工作表 我已经创建了一个代码,但它不起作用 Private Sub CommandButton2_Click() Application.ScreenUpdati

我已经创建了一个userform
frmNavigation
,它有一个
ListBox1
,它将列出工作簿中的所有工作表,我可以双击列表框中列出的任何工作表并转到该工作表

现在我有将近50个工作表,所以我从
ListBox1
中出现的列表中双击并转到该工作表,但现在我需要一个返回按钮“CommandButton2”,这样它可以将我带回以前的活动工作表

我已经创建了一个代码,但它不起作用

Private Sub CommandButton2_Click()

Application.ScreenUpdating = False

Dim i As Integer, Sht As String

'for loop
For i = 0 To ListBox1.ListCount - 1
    'get the name of the selected sheet
    If ListBox1.Selected(i) = True Then
        Sht = ListBox1.List(i - 1)
    End If
Next i

'select the sheet
Sheets(Sht).Select

'reset the userform
Unload Me
frmNavigation.Show

End Sub

尝试下面的代码,我不知道如何解释我下面代码的逻辑,我尽了最大努力在代码注释中描述它

我还修改了
ListBox1\u DblClick
code事件,以便在选择新工作表之前保存最新的
ActiveSheet

代码

Option Explicit

Dim LastSelectedSht As String ' Variable at module level, to store the name of the last selected sheet

'===================================================================

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 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

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 UserForm_Activate()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Sheets
    Me.ListBox1.AddItem ws.Name
Next ws

End Sub
选项显式
Dim LastSelectedSht As String”变量在模块级别,用于存储上次选定工作表的名称
'===================================================================

私有子命令按钮2_单击() 将TmpSht设置为字符串
TmpSht=ActiveSheet.Name“它给我的错误是行工作表(LastSelectedSht)上的下标超出范围。选择Private Sub UserForm_Initialize()Dim Sh as Variant”为每个循环在Active工作簿中为每个Sh添加可见的工作表。Sheets“将工作表添加到列表框me.ListBox1.AddItem Sh.Name下一步Sh@astha什么时候第一次加载表单时?Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)'将变量Application.screenupdate=False Dim i声明为整数,Sht声明为字符串LastSelectedSht=ActiveSheet.Name'for loop for i=0到ListBox1.ListCount-1'如果ListBox1.selected(i),则获取所选工作表的名称=True Then Sht=ListBox1.List(i)End If Next i'测试工作表是否已打开如果Active sheet.Name=Sht Then MsgBox“此工作表已打开!”如果选择工作表工作表(Sht),则退出子端。选择“重置用户表单卸载我FRMNNavigation.Show End SubPrivate Sub CommandButton2\u Click()将TmpSht设置为字符串TmpSht=ActiveSheet.Name'