Highlight separate areas?

London12F

Board Regular
Joined
Jun 21, 2016
Messages
59
The VBA below highlight 1 area for i=5 only.
How to modify VBA to highlight all 3 areas (for i=1,3,5)

Code:
Sub GroupArea()
   
   For i = 1 To 5 Step 2
   
   Range("B" & i & ":M" & (i + 1)).Select
   Next i


End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Re: How to highlight separate areas?

You'll need to use Union:

Code:
Sub GroupArea()
   
Dim selectionRange As Range
Dim newRange As Range
   
For i = 1 To 5 Step 2
    Set newRange = Range("B" & i & ":M" & (i + 1))
    If selectionRange Is Nothing Then
        Set selectionRange = newRange
    Else
        Set selectionRange = Application.Union(selectionRange, newRange)
    End If
Next i

selectionRange.Select

End Sub

WBD
 
Upvote 0
Re: How to highlight separate areas?

If the rows and columns are fixed, then you can do what you want with a single line of code...

Range("B1:M1,B3:M3,B5:M5").Select
 
Upvote 0

Forum statistics

Threads
1,217,364
Messages
6,136,115
Members
449,993
Latest member
Sphere2215

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top