2017-04-10 614 views
0

我創建了一個用戶窗體,其中有一個列表框(ListBox1),它列出了工作表名稱,我可以在列表中雙擊,它將帶我到該工作表。我有一個後退按鈕(CommandButton2),當我點擊後退按鈕時,它會將我帶到上一個選定的工作表。 我想鏈接我的列表框和後退按鈕,這樣當我點擊後退按鈕時,我的列表框(ListBox1)應該突出顯示我的後退按鈕(CommandButton2)所指向的工作表。Excel VBA用戶窗體顯示選中的工作表

請找我下面的代碼:

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 

回答

2

無需人的循環尋求選定的項目,可以按如下簡化事情有點:

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

@astha:上面的代碼是您在用戶窗體代碼窗格中所需的代碼/顯示功能的所有代碼。如果您需要幫助,只需詢問 – user3598756

+0

這是我完成它的方式,並且足夠動態以處理添加的任何工作表。 – Wowdude

+0

謝謝@Wowdude。現在讓我們等待astha的反饋! – user3598756

0

此子將在列表框中更改所選項目。

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的名字時新激活的工作表的名稱。

+0

我應該粘貼子SetListBox模塊中,並在空白按鈕(CommandButton2)呼叫類型SetListBox請指導,因爲我無法理解 – astha

相關問題