首页 文章

需要将用户选择的工作表名称分配给变量

提问于
浏览
2

我想提示用户选择工作表中的单元格来填充值 . 我正在尝试使用 Application.Input ,但它没有获取选中单元格的工作表的名称....它继续使用运行宏时处于活动状态的工作表名称 .

我到处搜索 . 有没有办法将sheetname分配给变量,以及用户在提示时选择的单元格范围?

varCellContent = Application.InputBox(prompt:="Choose a sheet by clicking on any cell in it.", Type:=8)
strDestinationSheetName = ActiveSheet.Name
MsgBox ("Sheet = " & strDestinationSheetName)

1 回答

  • 1

    这是你在尝试什么?

    Option Explicit
    
    Sub Sample()
        Dim varCellContent As Range
        Dim strDestinationSheetName As String
    
        On Error Resume Next
        Set varCellContent = Application.InputBox(Prompt:="Choose a sheet by clicking on any cell in it.", Type:=8)
        On Error GoTo 0
    
        If Not varCellContent Is Nothing Then
            strDestinationSheetName = varCellContent.Parent.Name
    
            MsgBox ("Sheet = " & strDestinationSheetName)
        End If
    End Sub
    

相关问题