highlighting specific cells using VBA?

ESACAWIP

New Member
Joined
Nov 9, 2020
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi, I was wondering if anyone can help me out.

I am looking for a vba solution where I have the ability to tell excel to:

-identify a starting cell (D1)
-at this point, begin highlighting a certain number of columns a random color (in this case, highlight every 2 columns and random color)
-end at cell (K4)


Can someone help me in creating such a vba code? Thank you very much!

excelhighlight.JPG
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
What range of random color? With random color (colorindex= 1 to 56) some time columns filled with dark color.
In this code, random color from 1 to 56.

VBA Code:
Option Explicit
Sub color()
Dim lr&, lc&, j&
lr = Cells(Rows.Count, "A").End(xlUp).Row
lc = Cells(1, Columns.Count).End(xlToLeft).Column
Randomize
For j = 4 To lc - 1 Step 2
    Cells(1, j).Resize(lr, 2).Interior.ColorIndex = Int(Rnd * 56) + 1
Next
End Sub
 
Upvote 0
This is quite similar to @bebo021999's but has a check to ensure that you do not end up with 4 or more consecutive columns the same colour.
It also adjusts the font colour so that it should always be fairly easy to read on whatever background cell colour is chosen.

VBA Code:
Sub Colour_Column_Pairs()
  Dim c As Long, lr As Long, OldClr As Long, NewClr As Long
  
  Const ColsToColour As String = "D:K"  '<- Set your column range here
  
  Randomize
  Application.ScreenUpdating = False
  lr = Range("A" & Rows.Count).End(xlUp).Row
  With Range(ColsToColour).Resize(lr)
    For c = 1 To .Columns.Count Step 2
      NewClr = Int(Rnd * 56) + 1
      Do Until NewClr <> OldClr
        NewClr = Int(Rnd * 56) + 1
      Loop
      With .Columns(c).Resize(, 2)
        .Interior.ColorIndex = NewClr
        .Font.Color = vbBlack
        Select Case NewClr
          Case 1, 3, 5, 9 To 14, 16, 18, 21, 23, 25, 29 To 32, 46 To 56
            .Font.Color = vbWhite
        End Select
      End With
      OldClr = NewClr
    Next
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
VBA Code:
VBA Code:

Hi thank you so much for the response! I was playing around with both of these vba codes and having some trouble figuring out how to change the number of columns that are highlighted. In both examples, 2 columns are highlighted / repeated. How would you change this number to e.g. 10 columns instead?

Thank you!
 
Last edited:
Upvote 0
Hi thank you so much for the response! I was playing around with both of these vba codes and having some trouble figuring out how to change the number of columns that are highlighted. In both examples, 2 columns are highlighted / repeated. How would you change this number to e.g. 10 columns instead?

Thank you!
My post in #2 with:
lc = Cells(1, Columns.Count).End(xlToLeft).Column
to define last column (with header in row 1).
Make sure your actua data columns have header then it should works.
 
Upvote 0
.. 2 columns are highlighted / repeated. How would you change this number to e.g. 10 columns instead?
Try this version. I have assumed that you would ensure that the number of columns in ColsToColour was a multiple of ColGroup.

Rich (BB code):
Sub Colour_Column_Groups()
  Dim c As Long, lr As Long, OldClr As Long, NewClr As Long
  
  Const ColsToColour As String = "D:R"  '<- Set your column range here
  Const ColGroup As Long = 5           '<- How many columns in each group
  
  Randomize
  Application.ScreenUpdating = False
  lr = Range("A" & Rows.Count).End(xlUp).Row
  With Range(ColsToColour).Resize(lr)
    For c = 1 To .Columns.Count Step ColGroup
      NewClr = Int(Rnd * 56) + 1
      Do Until NewClr <> OldClr
        NewClr = Int(Rnd * 56) + 1
      Loop
      With .Columns(c).Resize(, ColGroup)
        .Interior.ColorIndex = NewClr
        .Font.Color = vbBlack
        Select Case NewClr
          Case 1, 3, 5, 9 To 14, 16, 18, 21, 23, 25, 29 To 32, 46 To 56
            .Font.Color = vbWhite
        End Select
      End With
      OldClr = NewClr
    Next
  End With
  Application
 
Upvote 0
Solution
You're welcome. Glad we could help. Thanks for the follow-up.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,727
Members
449,049
Latest member
MiguekHeka

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