Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in


How to randomly add background/fill color for cells in Excel?

Let’s say you need to randomly add background or fill color for cells in the specified range, how to solve it? In this article, two methods are provided to get it done easily.

Randomly add background/fill color for cells with VBA

Randomly add background/fill color for cells with Kutools for Excel

Randomly insert values from the specified list or column (without repeats) in Excel

Kutools for Excel's Insert Random Data utility enables Excel users to randomly insert values from the specified list or column in Excel. Full Feature Free Trial 60-day!

ad randomly fill custom list 3

The following VBA macro code can help you to add background or fill color for each cell in a specific range with random colors. You can do as follows:

Step 1: Press Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

Step 2: Click Insert > Module, and then paste following VBA code into the opening Module window.

VBA: Add background or fill color for cells with random color

Sub TrimExcessSpaces()
Dim rng As Range
Dim WorkRng As Range
Dim xRed As Byte
Dim xGreen As Byte
Dim xBule As Byte
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each rng In WorkRng
xRed = Application.WorksheetFunction.RandBetween(0, 255)
xGreen = Application.WorksheetFunction.RandBetween(0, 255)
xBule = Application.WorksheetFunction.RandBetween(0, 255)
rng.Pattern = xlSolid
rng.PatternColorIndex = xlAutomatic
rng.Interior.Color = VBA.RGB(xRed, xGreen, xBule)
End Sub

Step 3: Press the F5 key or click the Run button to run this VBA macro.

Step 4: Now a dialog box comes out, just select the range you want to add background with random colors, and then click the OK button.

Now you will see all cells in the specified range are filled with different colors randomly. See below screen shot:

In some cases, you may need to add background color to randomly selected cells in a specified range. To get it done, you can try Kutools for Excel’s Sort Range Randomly utility, and then add background color for these random cells.

Kutools for Excel - Combines more than 300 Advanced Functions and Tools for Microsoft Excel

Step 1: Select the range where you want to find cells randomly and add background color.

Step 2: Click the Kutools > Range > Sort Range Randomly.

doc random background 3

Step 3: In the opening Sort Range Randomly dialog box, go to Select tab, type a number in the No. of cell(s) to select box, check Select random cells option, and click the OK button. See below screen shot:

Step 4: Now specified number of cells are randomly selected in the specified range. Then click the arrow besides the Fill Color button on the Home tab, and select a background color from the drop down list.

Now you will see the background color is added to the randomly selected cells as below screen shot shown:

doc random background 5

Kutools for Excel - Includes more than 300 handy Excel tools. Full feature free trial 60-day, no credit card required! Get it now!

In this Video, the Kutools tab and the Kutools Plus tab are added by Kutools for Excel. If need it, please click here to have a 60-day free trial without limitation!

Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Kennon · 1 years ago

    I am trying to do this same highlight macro with one small adjustment:is there a way to take the range selected an highlight it one random color so each time you run the macro the range of active cells is highlighted a different color

    Please help.