How To Copy data having date between two defined dates to a new worksheet using VBA?
Tags: tocopydatahavingdate, vbamacros
Published: 2021-10-05 11:14:06

---
title: How To Copy data having date between two defined dates to a new worksheet using VBA?
published: true
date: 2021-10-05 08:03:10 UTC
tags: ToCopyDataHavingDate,VBAMacros
canonical_url: https://geekexcel.com/how-to-copy-data-having-date-between-two-defined-dates-to-a-new-worksheet-using-vba/
---

In this tutorial, we will guide you on how to  **copy data having dates between two defined dates to a new Worksheet** 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  **three columns**. The first column contains **dates** , the second column contains **agent names** , and the third column contains **numbers of sales made by an agent** on that particular date.

![](https://geekexcel.com/wp-content/uploads/2021/10/Sample-data-12-1024x290.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 CopyDataBasedOnDate()
'Disabling screen updates
Application.ScreenUpdating = False
'Declaring two variables of Date data type
Dim StartDate, EndDate As Date
'Declaring variable for worksheet object
Dim MainWorksheet As Worksheet
'Initializing the Date variables with starting date from cell J8
'and end date from cell J9 of "Macro" sheet
StartDate = Sheets("Macro").Range("J8").Value
EndDate = Sheets("Macro").Range("J9").Value
'Initializing worksheet object with "RawData" worksheet
Set MainWorksheet = Worksheets("RawData")
'Activating the worksheet object
MainWorksheet.Activate
'Sorting the data by date in column A in ascending order
Range("A1").CurrentRegion.Sort _
      key1:=Range("A1"), order1:=xlAscending, _
       Header:=xlYes
'Filter the data based on date range between starting date and end date
Range("A1").CurrentRegion.AutoFilter Field:=1, Criteria1:= _
        ">=" & StartDate, Operator:=xlAnd, Criteria2:="<=" & EndDate
'Copy the filter data
ActiveSheet.AutoFilter.Range.Copy
'Inserting new worksheet after the last worksheet in the workbook
Worksheets.Add after:=Worksheets(Worksheets.Count)
'Pasting the copied data
ActiveSheet.Paste
'Auto adjusting the size of selected columns
Selection.Columns.AutoFit
Range("A1").Select
'Activating the "RawData" sheet
MainWorksheet.Activate
'Removing filter from the worksheet which we applied earlier
Selection.AutoFilter
Sheets("Macro").Activate
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-10.png)<figcaption>Save the Code</figcaption>

- Before **running the macro** , you have **two inputs** are required from a user. User has to **define the start and end date**. Based on the **specified dates** , the macro will **extract the data** between the **defined date range to a new worksheet**. After specifying the **start and end dates** , the user has to click the **Submit** button to execute the macro.

![](https://geekexcel.com/wp-content/uploads/2021/10/Macro-Data.png)<figcaption>Macro Data</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-10.png)<figcaption>Run the Code</figcaption>

- Finally, you will receive output as it will sort data in the **SampleData sheet** , based on date column and extract data based on the specified date range to the **newly inserted worksheet**.

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

## Verdict

We hope that this tutorial gives you guidelines on how to  **copy data having dates between two defined dates to a new Worksheet** 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/) **!!**

**Further Reference:**

- **[Excel Formulas to Highlight the Cells Between Two Dates!!](https://geekexcel.com/excel-formulas-to-highlight-the-cells-between-two-dates/)**
- **[How to Create User Defined Function through Excel VBA?](https://geekexcel.com/how-to-create-user-defined-function-through-excel-vba/)**
- **[Extract the Missing Values through Excel VBA](https://geekexcel.com/how-to-extract-the-missing-values-through-excel-vba/)**
- **[How to Copy a Formula in Excel Office 365 ~ Simple Guide!!](https://geekexcel.com/how-to-copy-a-formula-in-excel/)**
- **[How to Copy a Cell Formatting from One Cell to Other Cells in Excel 365?](https://geekexcel.com/how-to-copy-a-cell-formatting-from-one-cell-to-other-cells-in-excel-365/)**