How to automatically color rows and columns in Excel

To automatically color Excel columns and rows when clicking, we will use VBA.

In order to clarify the content in Excel data sheet, or to highlight the data, we have the option of alternating the Excel lines or coloring the alternate cells for Excel, depending on the content that needs attention at where. Then the color displays alternately for all data objects in the table.

In case the user does not want to interleave colors but only wants to get the color when clicking first, then you can use VBA code. In this way, the data table will highlight the values ​​in rows or columns when collated, by clicking the mouse when needed. The following article will show you how to automatically match color in Excel.

  1. How to change spreadsheet color lines in Excel
  2. How to format borders and background colors for Excel tables
  3. Change color between different lines in Microsoft Excel
  4. Instructions on how to create diagonal lines in Excel box

1. How to color Excel rows when you click

Step 1:

In the Excel file you need to process, click the Developer tab on the Ribbon and choose Visual Basic or press Alt + F11 to open the VBA code editor.

Picture 1 of How to automatically color rows and columns in Excel

Step 2:

Displaying the Microsoft Visual Basic for Applications interface, we click on the current sheet in Project VBAProject to open the Sheet dialog.

Picture 2 of How to automatically color rows and columns in Excel

Next, enter the following code into the interface and press Alt + Q to turn off this interface.

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) On Error Resume Next Cells.Interior.ColorIndex = 0 ActiveCell.EntireRow.Interior.ColorIndex = 8 Application.CutCopyMode = True End Sub 

Picture 3 of How to automatically color rows and columns in Excel

Step 3:

Back to the Excel data sheet interface when we click the mouse pointer on a cell, the row will be colored as shown below. Automatic rows are colored when you click.

Picture 4 of How to automatically color rows and columns in Excel

2. How to color Excel columns automatically

Step 1:

We also press Alt + F11 to open the VBA code editor. At this interface you continue to double-click on the currently open sheet in the Project VBAProject interface. Enter the code below into the VBA interface and also press Alt + Q to turn off this interface.

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) On Error Resume Next Cells.Interior.ColorIndex = 0 ActiveCell.EntireColumn.Interior.ColorIndex = 8 Application.CutCopyMode = True End Sub 

Picture 5 of How to automatically color rows and columns in Excel

Step 2:

As a result, when you click on any one cell, the column will be colored as shown below.

Picture 6 of How to automatically color rows and columns in Excel

3. How to color rows and columns in Excel

When you request to color columns or rows by value, use the above two ways. In case of comparison due to data comparison, we are forced to color both in rows and columns.

Step 1:

In the VBA code entry interface, you also click on the working sheet then enter 1 of the 2 code snippets below.

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) On Error Resume Next Cells.Interior.ColorIndex = 0 ActiveCell.EntireRow.Interior.ColorIndex = 8 ActiveCell.EntireColumn.Interior.ColorIndex = 8 Application.CutCopyMode = True End Sub 

Or use the code below if the code is problematic.

 Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) 'Update 20140318 Static xRow Static xColumn If xColumn <> "" Then With Columns(xColumn).Interior .ColorIndex = xlNone End With With Rows(xRow).Interior .ColorIndex = xlNone End With End If pRow = Selection.Row pColumn = Selection.Column xRow = pRow xColumn = pColumn With Columns(pColumn).Interior .ColorIndex = 6 .Pattern = xlSolid End With With Rows(pRow).Interior .ColorIndex = 6 .Pattern = xlSolid End With End Sub 

Picture 7 of How to automatically color rows and columns in Excel

Step 2:

After closing the interface to enter this VBA code, the user clicks any number and sees that both the column and the row passing through that cell are colored differently from the remaining cells.

Picture 8 of How to automatically color rows and columns in Excel

The color for the boxes when marked will depend on the ColorIndex color value table. To change the color of the column when highlighted, we need to change the number of values ​​shown for each color according to this table.

Picture 9 of How to automatically color rows and columns in Excel

To change the color we need to dim the interface to enter the code in VBA, then change the number of values ​​in ColorIndex = 8 to ColorIndex = 40 for example.

Picture 10 of How to automatically color rows and columns in Excel

Then the line color in Excel is converted to another color.

Picture 11 of How to automatically color rows and columns in Excel

With the type of color for the column and the user box can completely combine 2 different colors in the code.

Picture 12 of How to automatically color rows and columns in Excel

Excel column and row results have changed into 2 different colors as shown below.

Picture 13 of How to automatically color rows and columns in Excel

Another way to color rows and columns in Excel. Just enter the correct code, the row and column when clicked are all colored, easily collated, compared or searched for information in rows or columns, especially with long spreadsheets.

I wish you all success!

Update 21 June 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile