changing the color of each cell in a selection

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I wrote the code below to change the color of each cell in a selection. Do you think it is good and efficient code? How you would do it yourself. Thank you so much.

Code:
Sub myselection()
    Dim xcell As Range
    Dim i As Integer
    i = 1
    For Each xcell In Selection
        xcell.Interior.ColorIndex = i
        i = i + 1
    Next
End Sub

update: I just found that if the selection is big then the code will fail because colorindex will be more than 56. Hence, I will see how you would do it. Thank you
 
Last edited:

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
It's fine if that's what you're looking to do. Since there are only 56 color indices, you may want to change one line to

Code:
    xcell.Interior.ColorIndex = (i Mod 56) + 1

to prevent errors in case of a large range.
 
Upvote 0
Try
Code:
        i = IIf(i = 56, 1, i + 1)
 
Upvote 0
Also on the 56 colors:

Code:
Sub myselection()
    Dim xcell As Range, i As Long
    i = 1
    For Each xcell In Selection
        i = IIf(i < 56, i + 1, 1)
        xcell.Interior.ColorIndex = i
    Next
End Sub
 
Upvote 0
I got an error if more than 50 cells

Code:
Sub myselection()
Dim xcell As Range
Dim i As Integer: i = 1
    For Each xcell In Selection
        xcell.Interior.ColorIndex = i
        If i = 50 Then
           i = 1
         Else: i = i + 1
        End If
    Next xcell
End Sub

Note that if then you rigt-click the sheet name, select "view code" and paste

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call myselection
End Sub

Selecting cells on that sheet will automatically color them without having to launch the macro manually
 
Last edited:
Upvote 0
I got an error if more than 50 cells

Code:
Sub myselection()
Dim xcell As Range
Dim i As Integer: i = 1
    For Each xcell In Selection
        xcell.Interior.ColorIndex = i
        If i = 50 Then
           i = 1
         Else: i = i + 1
        End If
    Next xcell
End Sub

Note that if then you rigt-click the sheet name, select "view code" and paste

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Call myselection
End Sub

Selecting cells on that sheet will automatically color them without having to launch the macro manually

See post#2 from @Eric W

It's fine if that's what you're looking to do. Since there are only 56 color indices, you may want to change one line to
 
Upvote 0
It's probably worth noting that there are other ways to set a cell's color. You can also use the

xcell.Interior.Color = 12345

property. If you use that, the values can range from 0 to 256^3 - 1. It also makes sense to use the RGB function like this:

xcell.Interior.Color = RGB(10, 200, 40)

where each value in RGB varies from 0 to 255 and denotes the amounts of Red/Green/Blue to use in the mix. If you use the Macro Recorder while changing a cell's color, then look at the code, you'll see that there are other properties you can use to modify the color, like ThemeColor and TintAndShade.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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