2

是否可以从用户选择中获得多个范围。例如,如果用户选择了“A1:B2”,然后按住 ctrl 并选择了“E1:G2”,那么我可以将第一个范围设置为 1 Range 变量,将第二个范围设置为另一个?

4

2 回答 2

5

我认为Areas收藏是您正在寻找的。这是选择的两个非连续范围的示例:

Sub testing()
    'testing area 
    Range("A1:A10,E1:E10").Select

    Dim rngFirst As Range
    Dim rngSecond As Range

    Set rngFirst = Selection.Areas(1)
    rngFirst.Select

    'if there is no other separate range within selection to avoid errors
    'which could solved possible problems this way
    On Error Resume Next
    Set rngSecond = Selection.Areas(2)
    rngSecond.Select

End Sub
于 2013-05-29T15:17:32.260 回答
1

是的,这是可能的,这是一种方法:

Sub Selected__Ranges()
    Range("A1:B2,E1:G2").Select
    Dim rng
    Set rng = Selection
    Debug.Print rng.Address
End Sub
于 2013-05-29T15:15:43.110 回答