How to filter an excel date column using VBA?

I’m working on a VBA script in Excel that filters a range of cells and then prints the filtered rows. However, I’m encountering a runtime error and I’m not sure how to resolve it.

Date filtering : For the date filtering its supposed to only return row with dates from the previous day or the past 3 days if the current day is monday

so if today is friday the only rows visible should be from thursday and if its monday – sunday saturday and friday rows should appear (hope this makes sense)

VB script:

Set filterRange = srcSheet.Range("A1").CurrentRegion

' Apply the filter
With filterRange
    If Weekday(Date) = 2 Then ' If today is Monday
        .AutoFilter Field:=dateColumnIndex, Criteria1:=">=DATE(" & Year(Date - 3) & "," & Month(Date - 3) & "," & Day(Date - 3) & ")"
        .AutoFilter Field:=dateColumnIndex, Criteria2:="<=DATE(" & Year(Date - 1) & "," & Month(Date - 1) & "," & Day(Date - 1) & ")"
    Else ' If today is any other day
        .AutoFilter Field:=dateColumnIndex, Criteria1:="=DATE(" & Year(Date - 1) & "," & Month(Date - 1) & "," & Day(Date - 1) & ")"
    End If
    .AutoFilter Field:=statusColumnIndex, Criteria1:="LDP"
End With

' Copy the filtered data to the destination sheet
lastRow = filterRange.Resize(filterRange.Rows.Count - 1, filterRange.Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Row
filterRange.Resize(lastRow - filterRange.Row + 1, filterRange.Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Copy destSheet.Cells(1, 1)

' Clear the filters
filterRange.AutoFilter

When I run this script, I get a runtime error on this line:

lastRow = filterRange.Resize(filterRange.Rows.Count - 1, filterRange.Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Row

The error message is “Run-time error ‘1004’: No cells were found.”

However, when I print the number of visible rows to the debug console using

Debug.Print filterRange.Rows.SpecialCells(xlCellTypeVisible).Count,

I get 37, which suggests that the filter is working correctly and there are 37 visible rows.

But when I try to print the filtered rows using filterRange.PrintOut, it prints empty rows.
I’m not sure why I’m getting this error or why the PrintOut method isn’t printing the visible rows. Any help would be greatly appreciated!

Apologies for the bad code its my first day learning and writing VB using AI of course ;D

here is the full script for reference:

Public Sub FilterAndWriteData()
    Dim srcSheet As Worksheet
    Dim destSheet As Worksheet
    Dim lastRow As Long
    Dim filterRange As Range
    Dim dateColumnIndex As Long
    Dim statusColumnIndex As Long
    Dim visibleRows As Long
    Dim rngVisible As Range

    ' Set the source and destination worksheets
    ' Check if the "FilteredSheet" exists
    On Error Resume Next
    Set destSheet = ThisWorkbook.Worksheets("FilteredSheet")
    On Error GoTo 0

    ' If the "FilteredSheet" doesn't exist, create it
    If destSheet Is Nothing Then
        Set destSheet = ThisWorkbook.Worksheets.Add
        destSheet.Name = "FilteredSheet"
    End If

    ' Set the source worksheet
    Set srcSheet = ThisWorkbook.Worksheets("Sheet1")

    ' Find the column indices for date and status columns
    dateColumnIndex = srcSheet.Rows(1).Find(What:="Load Date", LookIn:=xlValues, LookAt:=xlWhole).Column
    statusColumnIndex = srcSheet.Rows(1).Find(What:="Final Status", LookIn:=xlValues, LookAt:=xlWhole).Column

    ' Clear the destination sheet
    destSheet.UsedRange.Clear

    ' Set the filter range
    Set filterRange = srcSheet.Range("A1").CurrentRegion

    ' Apply the filter
    With filterRange
        If Weekday(Date) = 2 Then ' If today is Monday
            .AutoFilter Field:=dateColumnIndex, Criteria1:=">=DATE(" & Year(Date - 3) & "," & Month(Date - 3) & "," & Day(Date - 3) & ")"
            .AutoFilter Field:=dateColumnIndex, Criteria2:="<=DATE(" & Year(Date - 1) & "," & Month(Date - 1) & "," & Day(Date - 1) & ")"
        Else ' If today is any other day
            .AutoFilter Field:=dateColumnIndex, Criteria1:="=DATE(" & Year(Date - 1) & "," & Month(Date - 1) & "," & Day(Date - 1) & ")"
        End If
        .AutoFilter Field:=statusColumnIndex, Criteria1:="LDP"
    End With

    visibleRows = filterRange.Rows.SpecialCells(xlCellTypeVisible).Count - 1
    Debug.Print "The size of the filtered data is: "; visibleRows

    ' Get the visible cells in filterRange
On Error Resume Next
Set rngVisible = filterRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
' Check if any visible cells were found
If Not rngVisible Is Nothing Then
    ' Print the visible cells
    rngVisible.PrintOut
Else
    MsgBox "No visible cells were found in the filter range."
End If

    ' Copy the filtered data to the destination sheet
    'lastRow = filterRange.Resize(filterRange.Rows.Count - 1, filterRange.Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Row
    'filterRange.Resize(lastRow - filterRange.Row + 1, filterRange.Columns.Count).Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Copy destSheet.Cells(1, 1)

    ' Clear the filters
    ' filterRange.AutoFilter
End Sub

I tried to filter using

With filterRange
        If Weekday(Date) = 2 Then ' If today is Monday
            .AutoFilter Field:=dateColumnIndex, Criteria1:=">=DATE(" & Year(Date - 3) & "," & Month(Date - 3) & "," & Day(Date - 3) & ")"
            .AutoFilter Field:=dateColumnIndex, Criteria2:="<=DATE(" & Year(Date - 1) & "," & Month(Date - 1) & "," & Day(Date - 1) & ")"
        Else ' If today is any other day
            .AutoFilter Field:=dateColumnIndex, Criteria1:="=DATE(" & Year(Date - 1) & "," & Month(Date - 1) & "," & Day(Date - 1) & ")"
        End If
        .AutoFilter Field:=statusColumnIndex, Criteria1:="LDP"
    End With

to filter by the columns and paste the results on a different sheet in the same workbook this is the overall aim of the script

New contributor

Lesego Matsimela is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật