如何在 Excel 宏中使用选定单元格的内容?

问题描述

我希望用户能够选择一个带有工作表名称的单元格,然后有一个宏按钮转到该工作表。如何让 VBA 使用所选单元格的内容

我有一个返回“摘要”工作表的设置,但希望让用户快速转到选定的工作表,而不必为每个工作表创建不同的宏,因为随着时间的推移,用户将向工作簿添加工作表.

Sub Return_to_Summary()

' Return_to_Summary Macro


         Sheets("Summary").Select

End Sub

解决方法

您可能还想添加一些验证或错误检查。

Public Sub Return_to_Summary()
Dim sName As String
Dim oSht As Worksheet
Dim bFound As Boolean
bFound = False

sName = ActiveCell.Text
For Each oSht In ActiveWorkbook.Sheets
    If oSht.name = sName Then
        bFound = True
        Exit For
    End If
Next

If bFound = True Then
    Sheets(ActiveCell.Text).Activate
Else
    MsgBox "This worksheet (" & sName & ") cannot be found. Check the spelling and capitalization."
End If
End Sub