Tested in Excel 2016 (16.0.9330.2073) 64-bit
You can use an input box to allow selecting a range and then get the workbook, sheet name and range.
To select a range, create a button on a sheet or form and add the following sub:
- Option Explicit
- Sub SelectRange()
- Dim a As String
- a = Application.InputBox("Please select...", , , , , , , 0)
- Debug.Print GetAddressPart(a, "w")
- Debug.Print GetAddressPart(a, "s")
- Debug.Print GetAddressPart(a, "r")
- End Sub
Where GetAddressPart function is:
- Public Function GetAddressPart(AddressString As String, Optional AddressPart As String = "R", Optional ConvertToStyle = xlA1) As String
- ' AddressPart could be W = Workbook; S = Sheet; R = Range
- ' TypeConvert could be xlA1 or xlR1C1
- Dim myWorkbook As String, mySheet As String, myRange As String
- Dim FromStyle, myRef
- ' find the workbook
- If InStr(1, AddressString, "]") > 0 Then
- myWorkbook = Mid(AddressString, InStr(1, AddressString, "[") + 1, InStr(1, AddressString, "]") - InStr(1, AddressString, "[") - 1)
- Else
- myWorkbook = ActiveWorkbook.Name
- End If
- ' find the sheet
- If InStr(1, AddressString, "]") > 0 Then
- mySheet = Mid(AddressString, InStr(1, AddressString, "]") + 1, InStr(1, AddressString, "!") - InStr(1, AddressString, "]") - 1)
- mySheet = Replace(mySheet, "'", "")
- Else
- If InStr(1, AddressString, "!") > 0 Then
- mySheet = Mid(AddressString, 2, InStr(1, AddressString, "!") - 2)
- mySheet = Replace(mySheet, "'", "")
- Else
- mySheet = ActiveSheet.Name
- End If
- End If
- ' find the range
- If ConvertToStyle = xlA1 Then
- FromStyle = xlR1C1
- Else
- FromStyle = xlA1
- End If
- myRef = Application.ConvertFormula(Mid(AddressString, 2, Len(AddressString)), FromStyle, ConvertToStyle)
- myRange = Mid(myRef, InStr(1, myRef, "$"), Len(myRef))
- ' return result
- Select Case UCase(AddressPart)
- Case "W"
- GetAddressPart = myWorkbook
- Case "S"
- GetAddressPart = mySheet
- Case "R"
- GetAddressPart = myRange
- End Select
- End Function