10

This is not the usedrange issue.
For example in Excel user selects a range (possibly empty) using mouse, let's say B4:C12

And let's say after this without deselecting the range user presses the macro, and macro should tell B4:C12.

Can anyone show example?

The macro should be something along the lines of the following:

Sub showrng()
    MsgBox SelectedRange.Address(ReferenceStyle:=xlA1)
End Sub
csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63
sdfg
  • 393
  • 2
  • 5
  • 9

3 Answers3

12
Sub macro1()
  MsgBox Selection.Address(ReferenceStyle:=xlA1, _
                           RowAbsolute:=False, ColumnAbsolute:=False)
End Sub

HTH!

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
4
Sub macro1()
  MsgBox Selection.Address
End Sub

or

Sub macro1()
    Dim addr as String
    addr = Selection.Address
    msgbox addr

    ' Now, as we found the address, according to that... you can also do other operations

End Sub
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
1

As selections can include several, independent ranges, the following code shows a more complete solution to the problem:

Public Sub SelectionTest()
Dim r As Range
Dim s As String

  Select Case Selection.Areas.Count
  Case 0:
    MsgBox "Nothing selected."
  Case 1:
    MsgBox "Selected range: " & Selection.Areas(1).Address(False, False)
  Case Else
    s = ""
    For Each r In Selection.Areas
      s = s + vbNewLine + r.Address(False, False)
    Next r
    MsgBox "Selected several areas:" & s
  End Select

End Sub
wribln
  • 355
  • 2
  • 13