How to do Conditional Formatting using VBA in Microsoft Excel?
Tags: todoconditionalforma, vbamacros
Published: 2021-10-07 12:09:12

---
title: How to do Conditional Formatting using VBA in Microsoft Excel?
published: true
date: 2021-10-06 11:14:11 UTC
tags: ToDoConditionalForma,VBAMacros
canonical_url: https://geekexcel.com/how-to-do-conditional-formatting-using-vba-in-microsoft-excel/
---

In this blog post, we are going to see how to  **do conditional formatting using VBA** in Microsoft Excel. Let’s get into this article!! Get an official version of MS Excel from the following link: **[https://www.microsoft.com/en-in/microsoft-365/excel](https://www.microsoft.com/en-in/microsoft-365/excel)**

## Syntax

```
If condition than
Range(range).Interior.ColorIndex= 1-56
```

## Example

- Firstly, you need to  **create a sample data**  with  **name, age and age group**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-data-15.png)<figcaption>Sample data</figcaption>

- In the Excel Worksheet, you have to go to the  **Developer Tab.**
- Then, you need to ** ** select the  **Visual Basic**  option under the  **Code**  section.

![Select Visual Basic](https://geekexcel.com/wp-content/uploads/2021/09/Select-Visual-Basic-9-1024x180.png)<figcaption>Select Visual Basic</figcaption>

- Now, you have to  **copy and paste the code**  given below.

```
Sub FormatUsingVBA()
Dim rng As Range
Dim lastRow As Long
lastRow = Cells(Rows.Count, 3).End(xlUp).Row
Set rng = Range("C2:C" & lastRow)For Each cell In rng
If cell.Value2 = "Adult" Then
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 3
ElseIf cell.Value2 = "KID" Then
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 4
ElseIf cell.Value2 = "Teenager" Then
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 6
Else
Range(cell.Address).Offset(0, -2).Interior.ColorIndex = 0
End If
Next cell
End Sub

```

- After that, you need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Save-the-Code-17.png)<figcaption>Save the Code</figcaption>

- Again, you have to go to the  **Excel Spreadsheet** , and click on the  **Developer Tab**.
- You need to choose the  **Macros option**  in the Code section.

![Choose Macro option](https://geekexcel.com/wp-content/uploads/2021/09/Choose-Macro-option-11-1024x184.png)<figcaption>Choose Macro option</figcaption>

- Now, you have to make sure that your  **macro name is selected**  and click the  **Run ** button.

![](https://geekexcel.com/wp-content/uploads/2021/10/Run-the-Code-16.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive **output** as given below in Microsoft Excel.

![](https://geekexcel.com/wp-content/uploads/2021/10/Output-14.png)<figcaption>Output</figcaption>

## Verdict

In the above article, you can learn the simple steps ** ** on how to  **do conditional formatting using VBA** in Microsoft Excel. Kindly, share your  **feedback**  in the below comment section. Thanks for visiting **[Geek Excel.](https://geekexcel.com/)** Keep Learning!

**Read Next:**

- **[Excel Formulas to Apply Conditional Formatting based on Another Cell!!](https://geekexcel.com/excel-formulas-to-apply-conditional-formatting-based-on-another-cell/)**
- **[Excel Formulas to Apply Conditional Formatting based on Another Column!](https://geekexcel.com/excel-formulas-to-apply-conditional-formatting-based-on-another-column/)**
- **[Formulas to Highlight Bottom Values with Conditional Formatting!!](https://geekexcel.com/excel-formulas-to-highlight-bottom-values-with-conditional-formatting/)**
- **[Excel Formulas to Highlight Last N Rows with Conditional Formatting!!](https://geekexcel.com/excel-formulas-highlight-last-n-rows-conditional-formatting/)**