Extract a worksheet name from the range selected

Sam picture Sam · Feb 8, 2017 · Viewed 9.7k times · Source

I have to input the result in the selected cell through InputBox function:

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

When I ran the code in the different worksheet and want the result in the different worksheet, it drops the result in the worksheet where I initially ran the code.

How do I get the Worksheet name, which I selected through the Application.InputBox ?

For example, when I selected in the Inputbox: Definitions!$F$38 how do I get the name 'Definitions'?

Answer

Fadi picture Fadi · Feb 8, 2017

Try This:

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