filter same column to get data between two dates

dss28

Board Regular
Joined
Sep 3, 2020
Messages
165
Office Version
  1. 2007
Platform
  1. Windows
I am trying to build a code to filter column D (date Column) to extract data between two dates which i specify in a userform in two textboxes (start and end date).

when I run the code i only get the headers in the newly created shet but rest of the data is not filtered/copied/pasted (?).

I tried date formats such as dd/mm/yyyy, mm/dd/yyyy, mmm/dd/yyyy, number format but I am getting only the header part.

request for help to sort the issue.

my code is as follows:


VBA Code:
Sub FilterBetweenDates()

'' get data between two dates from TotalReport and save in new work book


Application.ScreenUpdating = False

Dim X As Range

Dim rng As Range

Dim last As Long

Dim sht As String

Dim newBook As Excel.Workbook

Dim Workbk As Excel.Workbook



'Specify sheet name in which the data is stored

sht = "TotalReport"



'Workbook where VBA code resides

Set Workbk = ThisWorkbook



'New Workbook

Set newBook = Workbooks.Add(xlWBATWorksheet)

Workbk.Activate



'specify filter range in the following code

last = Workbk.Sheets(sht).Cells(Rows.Count, "B1:H").End(xlUp).row



With Workbk.Sheets(sht)

Set rng = .Range("B1:H" & last)

End With


' filtering code =========== date in column D ========================================

With Sheet19

With rng

.AutoFilter

.AutoFilter Field:=4, Criteria1:=">=" & UserForm36.TextBox1.Value, Operator:=xlAnd 'start date"

.AutoFilter Field:=4, Criteria2:="<=”&UserForm36.TextBox2.Value, Operator:=xlAnd 'end date"


'=============== copy=============================

With rng1

.SpecialCells(xlCellTypeVisible).Copy

'=====add new workbook and paste the filtered data============

Application.Workbooks.Add 1

newBook.Activate

Range("B3").Select

Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _

False, Transpose:=False

End With


Workbk.Sheets(sht).AutoFilterMode = False


With Application

.CutCopyMode = False

.ScreenUpdating = True


End With


End With


MsgBox " A new workbook has been created, name the file and save", vbOKOnly, "Report between Two Dates "


End With


End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Isn't column D the third field of .Range("B1:H" & last), not the fourth ?
 
Upvote 0
Isn't column D the third field of .Range("B1:H" & last), not the fourth ?
thanks @NoSparks
I tried both ways using 3 and 4 as filter fields criteria before but ... same no results ...
is the rest of the code correct?
 
Upvote 0
Try this and see if makes any difference:

VBA Code:
.AutoFilter Field:=3, Criteria1:=">=" & CLng(CDate(UserForm36.TextBox1.Value)), Operator:=xlAnd 'start date"

.AutoFilter Field:=3, Criteria2:="<=" & CLng(CDate(UserForm36.TextBox2.Value)), Operator:=xlAnd 'end date"
 
Upvote 0
Try this and see if makes any difference:

VBA Code:
.AutoFilter Field:=3, Criteria1:=">=" & CLng(CDate(UserForm36.TextBox1.Value)), Operator:=xlAnd 'start date"

.AutoFilter Field:=3, Criteria2:="<=" & CLng(CDate(UserForm36.TextBox2.Value)), Operator:=xlAnd 'end date"
no sir, same only header I can see in the sheet.
 
Upvote 0
I think there are other issues in the code but in terms of the filter itself try this:-
VBA Code:
            .AutoFilter Field:=3, Criteria1:=">=" & CLng(CDate(UserForm36.TextBox1.Value)), Operator:=xlAnd, _
                    Criteria2:="<=" & CLng(CDate(UserForm36.TextBox2.Value))
 
Upvote 0
No
I think there are other issues in the code but in terms of the filter itself try this:-
VBA Code:
            .AutoFilter Field:=3, Criteria1:=">=" & CLng(CDate(UserForm36.TextBox1.Value)), Operator:=xlAnd, _
                    Criteria2:="<=" & CLng(CDate(UserForm36.TextBox2.Value))
no success either,
can it be related to the date formatting in the sheet ?
 
Upvote 0
can it be related to the date formatting in the sheet ?
Formatting - No, as long at they are real dates (not text)
If you change the date format does the format actually change. If yes they are proper dates and the filter should work.

Can you provide an XL2BB of some sample data ?
Alternatively a copy of your spreadsheet via a sharing platform such as google drive, onedrive, dropbox, allowing anyone with the link to access the file and post the link here.

You might want to also post your full code (unless you provide the link to your spreadsheet). There are a number of things in your original code that don't make sense eg
The "End With"s,
the references to Sheet19 and to rng1
the "last =" line
 
Last edited:
Upvote 0
Using Option Explicit would be to your advantage.
Excel would then bring it to your attention that you don't have a rng1, which is what you're trying to use as the copy source.
 
Upvote 0
Formatting - No, as long at they are real dates (not text)
If you change the date format does the format actually change. If yes they are proper dates and the filter should work.

Can you provide an XL2BB of some sample data ?
Alternatively a copy of your spreadsheet via a sharing platform such as google drive, onedrive, dropbox, allowing anyone with the link to access the file and post the link here.

You might want to also post your full code (unless you provide the link to your spreadsheet). There are a number of things in your original code that don't make sense eg
The "End With"s,
the references to Sheet19 and to rng1
the "last =" line

thanks @NoSparks. Yes I have used "Option Explicit" in the code.

Yes @Alex Blakenburg, the dates are in date column is in Date format only and change when the format is changed.

I have been using the same code for other filters with no issue. The code I am using for filtering data between two dates is the one which is provided earlier.

Attached is the file for working as XL2BB.



trial.xlsm
ABCDEFGHIJ
1Sr NoYearHeading C DateHeading EHeading FHeading GHeading HHeading IHeading J
2120222022/00518-11-21aaazzzkkkggggggggg
3220212021/000119-11-21sssxxxlllhhhhhhhhh
4320212021/00220-11-21ddddccczzzjjjjjjjjj
5420212021/00321-11-21fffvvvxxxkkkkkkkkk
6520212021/00422-11-21gggbbbxxxkkkllllll
7620212021/00523-11-21hhhnnnccclllgggxxx
8720212021/00712-12-21jjjmmmvvvzzzhhhccc
9820212021/00613-12-21kkkqqqbbbxxxjjjvvv
10920212021/00814-12-21lllwwwnnnccckkkbbb
111020212021/01015-12-21zzzgggmmmvvvlllnnn
121120222022/00601-01-22xxxhhhqqqbbbcccmmm
131220222022/00701-02-22cccjjjwwwnnnvvvqqq
141320222022/00802-03-22vvvkkkgggmmmbbbwww
151420222022/01004-03-22bbblllhhhqqqnnnggg
161520222022/00305-04-22nnnkkkjjjwwwkkkhhh
171620222022/00106-05-22mmmlllkkkkkkllljjj
181720222022/00206-06-22qqqzzzllllllzzzkkk
191820222022/00407-07-22wwwxxxzzzzzzxxxlll
Sheet1
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top