【问题标题】:Activate sheet when clicking on cell based on cell value根据单元格值单击单元格时激活工作表
【发布时间】:2022-01-28 00:44:17
【问题描述】:

我试图设置一种方法,通过单击单元格并激活具有相同值的工作表来跳转到同一工作簿中的工作表。当单元格值为文本时,我的代码有效。但是,当值为数字时,它不起作用。大多数 (99%) 的工作表将仅是工作表名称的数字(6 位数字)。工作表名称的示例是 110110。这是我使用的代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B4:B800")) Is Nothing Then
If Target.Value <> "" Then
Sheets(Target.Value).Activate
End If
End If
End If
End Sub

我收到: 运行时错误“9”:

下标超出范围

每当我点击

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    在 Excel 中,您可以参考 sheet by the name or index

    在您的情况下,您指的是 Excel 中不存在的索引(数字)。 您可以使用 CStr() 简单地将数字转换为字符串:

    Sheets(CStr(Target.Value)).Activate
    

    【讨论】:

    • 谢谢!马上就修好了!
    猜你喜欢
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    相关资源
    最近更新 更多