KutoolsforOffice β€” One Suite. Five Tools. Get More Done.February Sale: 20% Off

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

AuthorKellyLast modified

When working with large datasets or presenting information in Excel, you may sometimes want to visually highlight your spreadsheet with randomly colored cells. For example, you might need to visually separate groups for analysis, create randomized quizzes, or just make your spreadsheet more engaging for review and discussion. Manually coloring each cell can be time-consuming and prone to mistakes. Fortunately, Excel offers reliable ways to quickly and randomly assign background or fill colors to cells within a specific range. In this article, we will introduce two effective methods you can use: applying a VBA solution or taking advantage of the specialized Kutools for Excel add-in.


Randomly add background/fill color for cells with VBA

Using VBA (Visual Basic for Applications), you can automatically assign a random fill color to each cell in a selected range. This is helpful when you want each cell to have a different color and you need to repeat this action frequently for different datasets.

1. Press Alt + F11 to open the Microsoft Visual Basic for Applications editor.

2. In the VBA editor, click Insert > Module to create a new module.

3. Copy and paste the following VBA code into the module window (do not edit the code):

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)
Next
End Sub

4. Press F5 or click the Run button to execute the macro.

5. When prompted, select the cell range you want to fill with random colors, and click OK.

6. The selected cells will be filled with randomly generated colors. To repeat for another range, run the macro again and select a new range.

VBA prompt to select the data range

Cells filled with different random colors

  • Macro security: If you see a security warning, enable macros for this workbook before running the code.
  • Randomness: Colors are assigned purely at random; duplicates can occur, especially in large ranges.
  • Performance: Very large selections may take a moment to process.
  • Name typos in code: The variable xBule is spelled as provided. It still works consistently in this macro; change only if you intend to rename it everywhere.
  • Troubleshooting: If nothing happens, ensure the workbook is not read-only and that the code was pasted into a standard module (not a sheet or ThisWorkbook module).


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

In many practical scenarios, you may prefer to randomly select just a subset of cells rather than the entire range, and then apply the background color of your choice. Kutools for Excel offers a user-friendly utility named Sort Range Randomly that simplifies this task without the need for VBA or formulas. This is especially helpful for users who want to add a splash of color to only a certain number of cells in a list or dataset β€” for instance, to highlight randomly selected winners, assign groups, or sample test data.

Kutools for Excel - Packed with over 300 essential tools for Excel. Enjoy permanently free AI features! Download now!

1. Select the entire range of cells where you want to randomly fill some with background color. For best results, ensure your data does not contain merged cells or other formatting that could affect selection.

2. Go to the Kutools tab on the ribbon, choose Range, then click Sort Range Randomly.

click Sort Range Randomly of kutools

3. In the dialog box, switch to the Select tab. Enter the number of cells you wish to select at random in the No. of cell(s) to select box, and check Select random cells. Click OK to confirm.

set options in the Sort Range Randomly dialog box

4. The specified number of cells will be highlighted (selected). To apply a fill color, go to the Home tab, click the arrow next to Fill Color, and choose a color. The selected cells will be colored immediately.

fill random selected cells with color

Kutools for Excel - Supercharge Excel with over 300 essential tools, making your work faster and easier, and take advantage of AI features for smarter data processing and productivity. Get It Now

This method is ideal if you wish to control the quantity of randomly colored cells and prefer to select your fill color. Kutools' approach avoids the need to write any code or use formulas, making the process accessible even to users less familiar with Excel's more advanced features. The main limitation is that it only assigns a single chosen color at a time; for fully randomized coloring of all cells, the VBA approach described earlier may be more suitable. If you need to re-select or change random cells, simply repeat the selection process using Sort Range Randomly, then apply a new color as needed.

  • Selection integrity: Avoid merged cells; they can interfere with random selection.
  • Undo-friendly: You can revert with Ctrl+Z if the selection/color isn’t what you wanted.
  • One color per pass: The tool applies one chosen color at a time. Repeat with a new selection/color if needed.
  • Performance tip: Very large ranges may take longer to process; narrow the selection if necessary.

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

 

Related articles:

Best Office Productivity Tools

πŸ€–Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |  Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |  Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!

All Kutools add-ins. One installer

Kutools for Office suite bundles add-ins for Excel, Word, Outlook & PowerPoint plus Office Tab Pro, which is ideal for teams working across Office apps.

ExcelWordOutlookTabsPowerPoint
  • All-in-one suite β€” Excel, Word, Outlook & PowerPoint add-ins + Office Tab Pro
  • One installer, one license β€” set up in minutes (MSI-ready)
  • Works better together β€” streamlined productivity across Office apps
  • 30-day full-featured trial β€” no registration, no credit card
  • Best value β€” save vs buying individual add-in