2010-09-10 100 views
7

這不是usedrange問題。
例如,在Excel中,用戶選擇使用鼠標範圍(可能爲空),讓我們說B4:C12Excel VBA通​​過鼠標獲取用戶所選範圍的範圍

而讓在此之後說,沒有取消的範圍內用戶按下宏,宏應該告訴B4:C12

任何人都可以顯示的例子嗎?

宏應遵循以下線的東西:

Sub showrng() 
    MsgBox SelectedRange.Address(ReferenceStyle:=xlA1) 
End Sub 

回答

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

HTH!

+1

1但ReferenceStyle:= xlA1將更緊密地匹配問題的要求 – barrowc 2010-09-11 00:28:23

3
Sub macro1() 
    MsgBox Selection.Address 
End Sub 

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 
1

作爲選擇可以包括幾個獨立的範圍內,下面的代碼顯示的問題的更完整的解決方案:

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