Need to copy cell reference from Application.Inputbox

  • I have coded a small vba Macro in the workbook named Destination.xlsx, and it is as follows:

    Code
    1. Sub GetDataArea()
    2. Dim rng As Range
    3. Set rng = Application.InputBox("Select the cells.", Type:=8)
    4. End Sub

    I have selected InputBox Type:=8 because I want users to select some cells and then I want to copy the cell reference or address of those selected cells. The problem here is that the Macro is coded in Destination.xlsx, I want the user to select cells in Source.xlsx (it can be any workbook of users' choice), and copy the cell reference or cell address of Source.xlsx. It looks like the screenshot attached here >InputBox

    Basically, I want to copy the text [Source.xlsx]SheetA!$A$1:$C$10 from my Inputbox. I am not sure how do I get this. I tried rng.address but it returns only $A$1:$C$10 While need complete reference as [Source.xlsx]SheetA!$A$1:$C$10

    Pls help...

  • This will do as you want


    Code
    1. Option Explicit
    2. Sub GetDataArea()
    3. Dim rng As Range
    4. On Error Resume Next
    5. Set rng = Application.InputBox("Select the cells.", Type:=8)
    6. On Error GoTo 0
    7. If rng Is Nothing Then Exit Sub
    8. rng.Copy
    9. MsgBox rng.Address
    10. End Sub


    You will need to save the Destination workbook as a macro enabled file -xlsm or xlsb

  • Hi royUK,


    Thanks for your reply.

    You are right about description of xlsx, xls, xlxm extentions.

    However, your prescribed solution did not work as desired. I need the user to select some cells in a worksheet other than the one where macro nis recorded. Hence, the inputbox will show the range address as [Source.xlsx]SheetA!$A$1:$C$10 which I want to copy or store in a variable, while your code displays msgbox with simple address as $A$1:$C$10 which I was anyways getting from my existing code.


    Do it like this, save macro in workbook1, run it, inputbox will appear in screen, switch to workbook2 and select some cells. It will show you the address of selected cells as [Source.xlsx]SheetA!$A$1:$C$10 and not as $A$1:$C$10.

  • Is this what you want?


  • Hi rouUK.

    Thanks for the quick revert.

    It still shows msgbox result as "Sheet1!$A$1". It does not show workbook name and shows only first cell of selection. It is very helpfull though. Can we add workbook name (file name) and if it can show full range selected?

    Thanks in advance for your quick help.