我想知道是否有任何VBA代码允许输入框使用下拉列表搜索工作表,并在用户搜索后自动重定向到工作表。类似于我们如何在excel上找到数据,除了它现在是纸张的事实。下拉列表应包含工作簿中的所有工作表,以便他们可以选择要访问的工作表。我有一个代码,用户必须手动输入,如果工作表名称太长,可能会很麻烦,从而导致拼写或格式错误。
Sub searchsheet()
Dim strsheet As String
' dim i as integer
strsheet = Application.InputBox("put sheet name", "Sheet Name Select", , , , , , 2)
With ThisWorkbook.Worksheets(strsheet)
.Select
.Activate
End With
End Sub
答案 0 :(得分:3)
这将在用户表单上显示一个组合框,但组合也可以放在工作表上
用户窗体:
Option Explicit
Private Sub ComboBox1_Change()
If Len(ComboBox1) > 0 Then Worksheets(ComboBox1.Text).Activate
End Sub
Private Sub ComboBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = vbKeyEscape Then Me.Hide
End Sub
Option Explicit
Public Sub navigateSheets()
Dim cmb As ComboBox, ws As Worksheet
Set cmb = UserForm1.ComboBox1
cmb.Clear
For Each ws In Worksheets
If ws.Visible Then cmb.AddItem ws.Name
Next
cmb.ListIndex = 0
setUserForm
End Sub
Private Sub setUserForm()
With UserForm1
.Height = 50: .Width = 111
.Caption = "Navigate Sheets"
With .ComboBox1
.BackColor = RGB(240, 250, 255)
.ControlTipText = "Select Sheet"
.Height = 17: .Width = UserForm1.Width - 15
.Top = 7: .Left = 7: .ListRows = 22
.MatchEntry = fmMatchEntryFirstLetter '0
End With
.Show
End With
End Sub
navigateSheets()
在可见工作表之间移动,并按Esc键关闭表单