3

I need to select multiple ranges in a worksheet to run various VBA code on them. The ranges will always begin on row 84 but the end depends on how far down the data goes. I've been selecting these ranges separately using code like this:

Sub SelectRange()
Dim LastRow As Integer
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("A84:B" & LastRow).Select
End Sub

That works fine, but I can't figure out how to select multiple ranges at once. I've tried everything I can think of:

  • Range("A84:B", "D84:E", "H84:J" & LastRow).Select
  • Range("A84:B,D84:E,H84:J" & LastRow).Select
  • Range("A84:B & LastRow,D84:E & LastRow,H84:J & LastRow").Select

Nothing works. I get a run-time error when running any of those.

2
  • 1
    Range("A84:B" & LastRow & ",D84:E" & LastRow & ",H84:J" & LastRow).Select Commented Oct 8, 2019 at 22:35
  • Range( Replace("A84:B<r>,D84:E<r>,H84:J<r>", "<r>", LastRow) ).Select Commented Oct 8, 2019 at 23:48

3 Answers 3

7

Use UNION:

Dim rng as Range
With ActiveSheet
    set rng = Union(.Range("A84:B" & LastRow),.Range("D84:E" & LastRow),.Range("H84:J" & LastRow))
End With
rng.select

But if you intend on doing something with that range then skip the .Select and just do what is wanted, ie rng.copy

Sign up to request clarification or add additional context in comments.

Comments

1

Put your dis-continued range address in the first argument of Range object.

For example, Range("A:A,D:D").Select will select column A and column D.

In your case, you may try:

Dim str As String, LastRow As Integer

LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
str = "A84:B" & LastRow & ",D84:E" & LastRow & ",H84:J" & LastRow
Range(str).Select

Comments

0

Range("A84:B & LastRow & "," & "D84:E & LastRow & "," & "H84:J & LastRow").Select

1 Comment

this answer returns a compile error. may due to the misplacing of double quotes. Try modify to Range("A84:B" & LastRow & "," & "D84:E" & LastRow & "," & "H84:J" & LastRow).Select

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.