【问题标题】:create a backbutton in excel vba userform to go to the previous active sheet在 excel vba 用户窗体中创建一个后退按钮以转到上一个活动表
【发布时间】:2017-04-09 17:56:44
【问题描述】:

我创建了一个用户表单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

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    试试下面的代码,我不知道如何解释下面代码的逻辑,我已经尽力在代码 cmets 中描述了。

    我还修改了ListBox1_DblClick 代码事件,以在您Select 新工作表之前保存最新的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
    

    【讨论】:

    • 它给我错误,因为下标超出了行 Sheets(LastSelectedSht).Select 的范围
    • Private Sub UserForm_Initialize() Dim Sh As Variant '为每个循环添加可见工作表 For Each Sh In ActiveWorkbook.Sheets '将工作表添加到列表框 Me.ListBox1.AddItem Sh.Name Next Sh跨度>
    • @astha 什么时候?第一次加载表单时?​​span>
    • Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) '声明变量 Application.ScreenUpdating = False Dim i As Integer, Sht As String LastSelectedSht = ActiveSheet.Name 'for loop For i = 0 To ListBox1 .ListCount - 1 '获取所选工作表的名称 If ListBox1.Selected(i) = True Then Sht = ListBox1.List(i) End If Next i '测试工作表是否已经打开 If ActiveSheet.Name = Sht Then MsgBox "这张单子已经打开了!” Exit Sub End If ' 选择工作表 Sheets(Sht).Select ' 重置用户表单 Unload Me frmNavigation.Show End Sub
    • Private Sub CommandButton2_Click() Dim TmpSht As String TmpSht = ActiveSheet.Name '
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多