VBA: automatically give color to next cell in a list/column if different from above/previous (distinct)

ooptennoort

Board Regular
Joined
Mar 29, 2021
Messages
67
Office Version
  1. 365
Platform
  1. Windows
To enhance visibility when looking through a list/column of slightly changing codes I want to make sure my eyes see when a code has changed, by coloring the distincts.

I want to: run a macro that gives my selection of cells a (unique?) (random?) color for each distinct cell value.

Anyone have a macro for this? Is it possible at all?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi & welcome to MrExcel.
Rather than separate colours how about alternating colours
VBA Code:
Sub ooptennoort()
   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then .Add Cl.Value, (.Count Mod 2) + 36
         Cl.Interior.ColorIndex = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
Rather than separate colours how about alternating colours
VBA Code:
Sub ooptennoort()
   Dim Cl As Range
  
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then .Add Cl.Value, (.Count Mod 2) + 36
         Cl.Interior.ColorIndex = .Item(Cl.Value)
      Next Cl
   End With
End Sub

Mmmh, and then it dawned on me why I could not find anything reg what I wanted ;) Thank you! You're right of course: alternating works just as well!!! Stupid of me ;)
Now your vba is hard coded for A2, but how would I write this to be applied to any cells (even if not bordering (i.e. adjacent)) that I select (selection)?
Thx again!
 
Upvote 0
How about
VBA Code:
Sub ooptennoort()
   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Selection
         If Not .Exists(Cl.Value) Then .Add Cl.Value, (.Count Mod 2) + 36
         Cl.Interior.ColorIndex = .Item(Cl.Value)
      Next Cl
   End With
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub ooptennoort()
   Dim Cl As Range
  
   With CreateObject("scripting.dictionary")
      For Each Cl In Selection
         If Not .Exists(Cl.Value) Then .Add Cl.Value, (.Count Mod 2) + 36
         Cl.Interior.ColorIndex = .Item(Cl.Value)
      Next Cl
   End With
End Sub
So simple yet sooo convenient! Thank you so much!
 
Upvote 0
You're welcome & thanks for the feedback.
It's possible to do different colours, as you wanted, the problem is some of them may be quite dark & so you would need to change the font colour as well.
 
Upvote 0
You're welcome & thanks for the feedback.
It's possible to do different colours, as you wanted, the problem is some of them may be quite dark & so you would need to change the font colour as well.
From a layman's perspective, writing vba to change the font too (nested IF in vba?) is most likely 'stacked complexity'. If that is true it might seem 'easier' maybe to 'just' not use dark colours? If possible, this would, however, limit the number of usable colours considerably & thereby perhaps limit the number of colourable (changed) cells? Unless the colouring would restart when that limit is reached, reusing colours? (Thinking out loud.) But this too is over my head and your solution is already great!
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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