How To Set up conditional formatting for an area using VBA in Excel?
Tags: tosetupconditionalfo, vbamacros
Published: 2021-10-08 01:52:47

---
title: How To Set up conditional formatting for an area using VBA in Excel?
published: true
date: 2021-10-07 08:51:42 UTC
tags: ToSetUpConditionalFo,VBAMacros
canonical_url: https://geekexcel.com/how-to-set-up-conditional-formatting-for-an-area-using-vba-in-excel/
---

In this tutorial, we will guide you on how to ** set up conditional formatting for an area using VBA** in Excel. Let’s get them below!! 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)**

## Example

- Firstly, you need to **create a sample data** with some **random numbers**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-data-16.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 ConditionalFormat()
With Selection
    'Delete any previous conditional formatting
    .FormatConditions.Delete
    
    'add conditional formatting to selected cell
    .FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=" & Selection.Cells(1).Address(False, False) & "=MAX(" & Selection.Address & ")"
    
    'Assigning Violet color for the conditional formatting
    .FormatConditions(1).Interior.ColorIndex = 39
    
End With 
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-23-1024x453.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>

- Then, 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-22.png)<figcaption>Run the Code</figcaption>

- Now, you have to **select a range of cells from B9 to F17** as the **sample of selected data.**
- On **executing the macro after selecting a sample area** , conditional formatting is applied on the **selected area and cell D16** , which contains the **highest value (97)**, will be **highlighted in violet color**.

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

- You need to **take another sample** and **select the range from cell B10 to F13** as second sample area.
- After **selecting the second sample area** , you have to click on **Find Max button** to run the macro.
- Now, **conditional formatting** will be applied to the **selected area and cell C11** will be highlighted in **violet color** , as it contains the **maximum value (92)** in the selected area.

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

- Finally, after **executing the macro for two different samples** , **conditional formatting is applied to both**  **the cells C11 and D16** , and it will be **highlighted in violet color** , as they represent maximum values in their respective samples.

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

## Closure

We hope that this tutorial gives you guidelines on how to  **set up conditional formatting for an area using VBA** in Excel. Please leave a comment in case of any  **queries,**  and don’t forget to mention your valuable  **suggestions**  as well. Thank you so much for Visiting Our Site!! Continue learning on **[Geek Excel](https://geekexcel.com/)!! **Read more on [**Excel Formulas**](https://geekexcel.com/excel-formula/) **!!**

**Keep Reading:**

- **[Excel Formulas to Calculate the Average of Numbers!!](https://geekexcel.com/excel-formulas-to-calculate-the-average-of-numbers/)**
- **[Excel Formulas to Get the Largest Value with Criteria!!](https://geekexcel.com/excel-formulas-to-get-the-largest-value-with-criteria/)**
- **[Formulas to Apply Conditional Formatting based on Another Cell!!](https://geekexcel.com/excel-formulas-to-apply-conditional-formatting-based-on-another-cell/)**
- **[How to do Conditional Formatting using VBA in Microsoft Excel?](https://geekexcel.com/how-to-do-conditional-formatting-using-vba-in-microsoft-excel/)**
- **[Excel Formulas to Apply Conditional Formatting based on Another Column!](https://geekexcel.com/excel-formulas-to-apply-conditional-formatting-based-on-another-column/)**