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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,361
Messages
6,136,103
Members
449,991
Latest member
IslandofBDA

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