Macro to select block of cells based on reference and hightlight cells in alternating colour

Jack_Mason

New Member
Joined
Nov 19, 2020
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hello, I have reference cells in column A which multiple rows in a block can be the same reference value. They arent in a numerical order so cant select evens/odds but I want to highlight the alternating ‘blocks’ of reference cells to make it easier to see the change in reference. If someone could help me with the code. I would like it to highlight columns A-K for each block
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
How about this?

Book1 (version 2).xlsb
ABCDEFGHIJK
1A90982355404161252758
2A73707786178731806585
3A85615913773840192165
4B74207788322645783689
5B40117822774498631765
6C2596775612273591679
7C4873169391168499145
8C7754798157358992097
9C7284954686942027598
10D4576100181134483770
11D3822919939677113740
12E873520146181772580
13E7012601718100841523
14E6647343523468668059
15E40253826210022388147
16F757957491004751151
17F43261983829964851057
18F8285344310046428153
19G36857629318699718147
20G7487924751364836418
21G3549918934984956284
Sheet6


VBA Code:
Sub ALT()
Dim AR() As Variant: AR = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim s As String: s = "A1:"
Dim tmp As String: tmp = AR(1, 1)
Dim b As Boolean: b = True

For i = 2 To UBound(AR)

If AR(i, 1) <> tmp Then
    If b Then
        s = s & "K" & i - 1 & ","
        tmp = AR(i, 1)
    Else
        s = s & "A" & i & ":"
        tmp = AR(i, 1)
    End If
    b = Not b
End If

Next i
If Right(s, 1) = ":" Then
    s = s & "K" & i - 1
Else
    s = Left(s, Len(s) - 1)
End If
Range(s).Interior.Color = RGB(225, 225, 225)
End Sub
 
Upvote 0
How about this?

Book1 (version 2).xlsb
ABCDEFGHIJK
1A90982355404161252758
2A73707786178731806585
3A85615913773840192165
4B74207788322645783689
5B40117822774498631765
6C2596775612273591679
7C4873169391168499145
8C7754798157358992097
9C7284954686942027598
10D4576100181134483770
11D3822919939677113740
12E873520146181772580
13E7012601718100841523
14E6647343523468668059
15E40253826210022388147
16F757957491004751151
17F43261983829964851057
18F8285344310046428153
19G36857629318699718147
20G7487924751364836418
21G3549918934984956284
Sheet6


VBA Code:
Sub ALT()
Dim AR() As Variant: AR = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim s As String: s = "A1:"
Dim tmp As String: tmp = AR(1, 1)
Dim b As Boolean: b = True

For i = 2 To UBound(AR)

If AR(i, 1) <> tmp Then
    If b Then
        s = s & "K" & i - 1 & ","
        tmp = AR(i, 1)
    Else
        s = s & "A" & i & ":"
        tmp = AR(i, 1)
    End If
    b = Not b
End If

Next i
If Right(s, 1) = ":" Then
    s = s & "K" & i - 1
Else
    s = Left(s, Len(s) - 1)
End If
Range(s).Interior.Color = RGB(225, 225, 225)
End Sub
Thank you, I get a bug with the final line (method ‘Range’ of object’_global’ failed) error 1004?
 
Upvote 0
Can you post a sample of your data? It seems to be working fine with the sample data I posted.
 
Upvote 0
Cool. Here's a slightly cleaned up version of the code.

VBA Code:
Sub ALT()
Dim AR() As Variant:    AR = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Value2
Dim s As String:        s = "A1:"
Dim tmp As String:      tmp = AR(1, 1)
Dim b As Boolean:       b = True

For i = 2 To UBound(AR)

If AR(i, 1) <> tmp Then
    If b Then
        s = s & "K" & i - 1 & ","
    Else
        s = s & "A" & i & ":"
    End If
    tmp = AR(i, 1)
    b = Not b
End If

Next i

If b Then
    s = s & "K" & i - 1
Else
    s = Left(s, Len(s) - 1)
End If

Range(s).Interior.Color = RGB(225, 225, 225)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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