One of the Excel queries I often get is – “How to highlight the Active Row and Column in a data range?”
And I got one last week too.
So I decided to create a tutorial and a video on it. It will save me some time and help the readers too.
Below is a video where I show how to highlight the active row and column in Excel.
Let me first show you what we are trying to achieve.
In the above example, as soon as you select a cell, you can see that the row and column also get highlighted. This can be helpful when you’re working with a large dataset and can also be used in Excel Dashboards.
Now let’s see how to create this functionality in Excel.
Highlight the Active Row and Column in Excel
Here are the steps to highlight the active row and column on selection:
- Select the data set in which you to highlight the active row/column.
- Go to the Home tab.
- Click on Conditional Formatting and then click on New Rule.
- In the New Formatting Rule dialog box, select “Use a formula to determine which cells to format”.
- In the Rule Description field, enter the formula: =OR(CELL(“col”)=COLUMN(),CELL(“row”)=ROW())
- Click on the Format button and specify the formatting (the color in which you want the row/column highlighted).
- Click OK.
The above steps have taken care of highlighting the active row and active column (with the same color) whenever there is a selection change event.
However, to make this work, you need to place a simple VBA code in the backend.
Here is the VBA code that you can copy and paste (exact steps also listed below):
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Application.CutCopyMode = False Then Application.Calculate End If End Sub
The above VBA code is run whenever there is a selection change in the worksheet. It forces the workbook to recalculate, which then forces the conditional formatting to highlight the active row and the active column. Normally (without any VBA code) a worksheet refreshes only when there is a change in it (such as data entry or edit).
Also, an IF statement is used in the code to check if the user is trying to copy paste any data in the sheet. During copy paste, the application is not refreshed and it is allowed.
Here are the steps to copy this VBA code in the backend:
- Go to the Developer tab (can’t find the developer tab? – read this).
- Click on Visual Basic.
- In the VB Editor, on the left, you will see the project explorer that lists all the open workbooks and the worksheets in it. If you can’t see it, use the keyboard shortcut Control + R.
- With your workbook, double-click on the sheet name in which you have the data. In this example, the data is in Sheet 1 and Sheet 2.
- In the code window, copy and paste the above VBA code. You’ll have to copy and paste the code for both sheets if you want this functionality in both sheets.
- Close the VB Editor.
Since the workbook has VBA code in it, save it with a .XLSM extension.
Note that in the steps listed above, the active row and column would get highlighted with the same color. If you want to highlight the active row and column in different colors, use the below formulas:
In the download file provided with this tutorial, I have created two tabs, one each for single color and dual color highlighting.
Since these are two different formulas, you can specify two different colors.
- This method would not impact any formatting/highlighting you have done manually to the cells.
- Conditional formatting is volatile. If you use it on very large datasets, it may lead to a slow workbook.
- The VBA code used above would refresh the workbook every time there is a change in selection.
- CELL Function is available in Excel 2007 and above version for Windows and Excel 2011 and above for Mac. In case you’re using an older version, use this technique by Chandoo.
Want to Level-up your Excel Skills? Consider joining one of my Excel courses:
You May Also Like the Following Excel Tutorials:
- How to Highlight Blank Cells in Excel.
- Highlight Rows Based on a Cell Value in Excel.
- Highlight EVERY Other ROW in Excel.
- How to Move Rows and Columns in Excel.
- Creating Heatmap in Excel.
- How to Count Colored Cells in Excel.
- 24 Useful Macro Examples.
- Using Loops in Excel VBA.