gpt4 book ai didi

vba - 输入框搜索工作表

转载 作者:行者123 更新时间:2023-12-04 22:02:37 24 4
gpt4 key购买 nike

我想知道是否有任何 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

最佳答案

这将在用户表单上显示一个组合框,但该组合也可以放在工作表上

用户窗体:

user form with combo box

  • 创建一个默认名称为“UserForm1”(任意大小)的新用户窗体
  • 创建一个默认名称为“ComboBox1”(任意大小)的新组合框
  • 将此代码添加到表单 VBA 模块:

  • 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

  • 创建一个新的标准 VBA 模块并添加此代码:

  • 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 关闭表单
  • 关于vba - 输入框搜索工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086107/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com