2

Is it possible to get multiple ranges from the user selection. For example if the user has selected "A1:B2" then held down ctrl and selected "E1:G2" too then can i get the first range to 1 Range variable and the second one to another?

3
  • 2
    It is possible. Here on SO you are expected to give us an example of what you've already tried so that our answers will be as helpful as possible. Please show us the relevant code, and tell us what else you'd like it to do. Commented May 29, 2013 at 15:12
  • 1
    @DougGlancy i would normally say the same thing...but if think about this question, it may be very difficult for a beginner to figure this out. Commented May 29, 2013 at 15:17
  • The only thing i could think of is to ask the user to input the ranges in input boxes but i would like to do it with just the Selection. The final goal is to multiply two matrices from the Selection. Commented May 29, 2013 at 15:23

2 Answers 2

5

I think Areas collection is what you are looking for. Here is sample for two non continuous ranges selected:

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
Sign up to request clarification or add additional context in comments.

Comments

1

Yes its possible, this is one way to do it:

Sub Selected__Ranges()
    Range("A1:B2,E1:G2").Select
    Dim rng
    Set rng = Selection
    Debug.Print rng.Address
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.