Print Date: 2024-09-12
This concept map provides a simple explanation of the use of Excel VBA with examples.
I demonstrate how to begin using VBA through one of my primary use cases: highlighting the row of the active, selected cell. In this example, I use a Parkinson’s Glossary Map XLSM file1. The code is also found in Sheet 2 of the Workbook.
VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static xRow
If xRow <> “” Then With Rows(xRow).Interior .ColorIndex = xlNone End With End If
Active_Row = Selection.Row xRow = Active_Row With Rows(Active_Row).Interior .ColorIndex = 20 .Pattern = xlSolid End With
End Sub
Instruction to Highlight Active Row:
Copy code from the this file or from Cell B2 in Sheet “Highlight Active Row”
Right Click Selected Sheet, then select View Code
Paste code in the Selected Project Sheet. You’ll find all open Excel
Workbooks on the left side Project - VBA Project
menu
Selected Cell is on Cell C1 of Row 1.
Keep things in mind:
Save your file as .xlsm
instead of .csv
or .xlsx
to keep the VBA code intact after closing the
workbook. You will generally receive a notification regarding this
matter.
This specific code highlights the row of the active cell when a selection change occurs, as indicated in the code. This is applied to whichever sheet(s) you manually choose.
Caution: This particular code does override any filled-in colors (e.g., Column C) applied via the Fill Color Paintbox icon (blue), leaving the row of the selected cell without any filled-in colors after exiting and selecting outside that row. However, this code does not override Conditional Formatting (e.g., Column D and Column F).
It’s possible—most things are—to resolve these issues and improve further, but doing so requires time for exploration.
Selected Cell is on Cell C1 of Row 4, resulting in removed filled-in color from Row 1. Notice the Conditional Formatting colors have not changed.
Selected Cells were on Row 8 thru 11. While the cells in the image are set on Column C, you can choose any cell on that row and the filled-in color will end up being removed.
Tham, K. (2024). Excel VBA Example Workbook XLSM. Google Drive. https://drive.google.com/drive/folders/1mbMap3GOIZBJBEfiPq1zVIrV_w4BUXJY?usp=sharing.↩︎