How To Delete all cells with data from previous dates using VBA?
Tags: todeleteallcellswith, vbamacros
Published: 2021-10-06 09:56:51

---
title: How To Delete all cells with data from previous dates using VBA?
published: true
date: 2021-10-05 10:37:36 UTC
tags: ToDeleteAllCellsWith,VBAMacros
canonical_url: https://geekexcel.com/how-to-delete-all-cells-with-data-from-previous-dates-using-vba/
---

In this article, you will learn how to **delete all cells with data from previous dates using VBA** in Microsoft 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 **date, agent name and sales of the individual agent** on that particular date.

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

```
Option Explicit

Sub RemovePreviousData()

Dim Counter, LastRow As Integer

'Finding the row number of last row
LastRow = ActiveSheet.Range("A1").SpecialCells(xlLastCell).Row

'Looping from last row to 11th row
For Counter = LastRow To 11 Step -1
    If Cells(Counter, 1).Value < Date Then
        'Deleting the row
        Rows(Counter).Delete
    End If
Next Counter

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

- Finally, you will receive **output** as **deleted all cells with data from previous dates** as given below.

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

## Summary

We hope that this tutorial gives you guidelines on how to **delete all cells with data from previous dates 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/) **!!**

**Read Next:**

- **[How to Delete Sheet in Microsoft Excel 365?](https://geekexcel.com/how-to-delete-sheet-in-microsoft-excel-365/)**
- **[Quick Shortcut to Delete Cells in Microsoft Excel 365!!!](https://geekexcel.com/quick-shortcut-to-delete-cells-in-microsoft-excel-365/)**
- **[Shortcut to Delete Comment in Microsoft Excel 365](https://geekexcel.com/shortcut-to-delete-comment-in-microsoft-excel-365/)**
- **[Simple Shortcut to Delete Rows in Microsoft Excel 365!!](https://geekexcel.com/simple-shortcut-to-delete-rows-in-microsoft-excel-365/)**
- **[Simple Shortcut to Delete Columns in Microsoft Excel 365!!](https://geekexcel.com/simple-shortcut-to-delete-columns-in-microsoft-excel-365/)**