【问题标题】:Extract a worksheet name from the range selected从所选范围中提取工作表名称
【发布时间】:2017-02-08 22:11:45
【问题描述】:

我必须通过 InputBox 函数在所选单元格中输入结果:

Set OutputStrt = Application.InputBox("Select a cell, where the output should be dropped.", "Output start cell", Type:=8)

当我在不同的工作表中运行代码并希望在不同的工作表中得到结果时,它会将结果放到我最初运行代码的工作表中。

如何获取通过 Application.InputBox 选择的 Worksheet 名称?

例如,当我在输入框中选择:Definitions!$F$38 时,我如何获得名称“Definitions”?

【问题讨论】:

  • OutputStrt.Parent.Name

标签: excel worksheet inputbox vba


【解决方案1】:

试试这个:

Sub test()
    Dim Ws As Worksheet
    Dim OutputStrt As Range
    Set OutputStrt = Application.InputBox("Select a cell, where the output should be dropped.", "Output start cell", Type:=8)
    Set Ws = OutputStrt.Worksheet
    MsgBox Ws.Name
End Sub

【讨论】:

  • 谢谢! .Name 是我需要的。
  • 遇到了同样的问题。感谢您的回答。似乎如果您选择另一个工作簿,则不必指定它。 @Fadi
【解决方案2】:

你也可以用 Selection 对象调用它

Dim ws As Workseet
Set ws = Selection.Worksheet
Debug.Print "Sheetname: " & ws.Name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2013-08-19
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多