Highlight Active Row and Column in Excel (VBA)

Sumit Bansal
Written by
Sumit Bansal
Sumit Bansal

Sumit Bansal

Sumit Bansal is the founder of TrumpExcel.com and a Microsoft Excel MVP. He started this site in 2013 to share his passion for Excel through easy tutorials, tips, and training videos, helping you master Excel, boost productivity, and maybe even enjoy spreadsheets!

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.

Reader Query - How to Highlight Active Row and Column

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.

In case you prefer written instructions, below is a tutorial with exact steps on how to do it.

Let me first show you what we are trying to achieve.

A Demo to Show how to highlight the active row and column on selection change

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.

Download the Example File

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):

'Code developed by Sumit Bansal from https://trumpexcel.com
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).
Developer tab in the ribbon
  • Click on Visual Basic.
Visual Basic icon in the Ribbon
  • 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.
Vb Editor Project Explorer - Highlight Selected Row or Column
  • 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.
Vb Code in the backend to highlight the active row and column in Excel
  • Close the VB Editor.

Since the workbook has VBA code in it, save it with a .XLSM extension.

Download the Example File.

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:

  • =COLUMN()=CELL(“col”)
  • =CELL(“row”)=ROW()

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.

Useful Notes:

  1. This method would not impact any formatting/highlighting you have done manually to the cells.
  2. Conditional formatting is volatile. If you use it on very large datasets, it may lead to a slow workbook.
  3. The VBA code used above would refresh the workbook every time there is a change in selection.
  4. 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:

Hey! I'm Sumit Bansal, founder of trumpexcel.com and a Microsoft Excel MVP. I started this site in 2013 because I genuinely love Microsoft Excel (yes, really!) and wanted to share that passion through easy Excel tutorials, tips, and Excel training videos. My goal is straightforward: help you master Excel skills so you can work smarter, boost productivity, and maybe even enjoy spreadsheets along the way!

81 thoughts on “Highlight Active Row and Column in Excel (VBA)”

  1. Very Good and thank you.
    One small issue was that i found copying the formula in it’s BOLD format caused it not to work. I had to re-write it in normal text and it worked fine.

    Reply
  2. Searching this long grt tutorial and suffer one error also but with someone comment i change according n its work

    Reply
    • here he typed:
      =OR(CELL(“col”)=COLUMN(),CELL(“row”)=ROW())

      everything needs to be caps so:
      =OR(CELL(“COL”)=COLUMN(),CELL(“ROW”)=ROW())

      Reply
  3. thanks for your efforts, but i think Microsoft should add this as a basic excel services , no need to do this long steps

    Reply
  4. This is what I wanted. But, it only works on 1 worksheet in my workbook of 10 sheets. I have copied the VBA code on 2 or 3 sheets to see if that works, but still only works on 1 sheet. I would like to be able to not only work on all sheets, but also any other workbook file I need to use it in. Can you help me? Also, is it possible to make this an Excel Add-in, so it would work in any file I open?

    Reply

Leave a Comment

Get the FREE 51 Excel Tips Ebook

Enter your details and the free PDF is on its way to your inbox.

Hmm, that didn't go through. Please check your email and try again.

No spam. You'll also get my weekly Excel newsletter. Unsubscribe anytime.

Check your inbox!

The ebook is on its way to your email. It usually lands within a couple of minutes.