How To Use Cell protection only for formulas using VBA in Excel?
Tags: tousecellprotectiono, vbamacros
Published: 2021-10-05 02:49:02

---
title: How To Use Cell protection only for formulas using VBA in Excel?
published: true
date: 2021-10-04 09:18:21 UTC
tags: ToUseCellProtectionO,VBAMacros
canonical_url: https://geekexcel.com/how-to-use-cell-protection-only-for-formulas-using-vba-in-excel/
---

In this article, you will learn how to  **use cell protection only for formulas using VBA** in Microsoft Excel. Let’s see them below!! Get the 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 an employee sales data,** which includes **bifurication by month and year**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-data-10.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.

```
'Add below code in worksheet module
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
For Each rng In Target.Cells
    If rng.HasFormula Then
        ActiveSheet.Protect
    Else
        ActiveSheet.Unprotect
   End If
Next rng
End Sub

```

- You need to  **save the code**  by selecting it and then  **close the window**.

![](https://geekexcel.com/wp-content/uploads/2021/10/Save-the-Cde.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.
- Finally, you need to find out **total sales by month** , as well as by **year** as given below.

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

## Wrap-Up

We hope that this tutorial gives you guidelines on how to  **use cell protection only for formulas using VBA** in Microsoft 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:**

- **[Highlight Unique Values in Selection using Macros in Excel 365!!](https://geekexcel.com/highlight-unique-values-in-selection-using-macros-in-excel-365/)**
- **[How to Find Last non-blank Row and Column Using VBA?](https://geekexcel.com/how-to-find-last-non-blank-row-and-column-using-vba/)**
- **[How to Open and Close Excel Workbook using Excel VBA?](https://geekexcel.com/how-to-open-and-close-excel-workbook-using-excel-vba/)**
- **[Hide columns using a button in Excel VBA](https://geekexcel.com/how-to-hide-columns-using-a-button-in-excel-vba/)**
- **[How To Mail the selection using VBA in Microsoft Excel?](https://geekexcel.com/how-to-mail-the-selection-using-vba-in-microsoft-excel/)**