Hi All,
I have in my xlsm file the below VBA code that filters data of a source workbook xlsx by today's date in hidden mode and pastes those results in a destination xlsm workbook.
The VBA code works perfectly fine, however I don't get any msg box when no results is found. Any hint on that as to how I will fix it will be very helpful
Best Regards
I have in my xlsm file the below VBA code that filters data of a source workbook xlsx by today's date in hidden mode and pastes those results in a destination xlsm workbook.
The VBA code works perfectly fine, however I don't get any msg box when no results is found. Any hint on that as to how I will fix it will be very helpful
VBA Code:
Sub FilterTableByDate()
Dim sourceWorkbook As Workbook
Dim destinationWorkbook As Workbook
Dim sourceWorksheet As Worksheet
Dim destinationWorksheet As Worksheet
Dim sourceTable As ListObject
Dim destinationTable As ListObject
Dim todayDate As Date
Dim filterRange As Range
'Set the source and destination workbook and worksheet
Set destinationWorkbook = ThisWorkbook 'Assuming the code is in the destination workbook
Set destinationWorksheet = destinationWorkbook.Worksheets("Today's Deliveries")
'Open the source workbook in hidden mode
Set sourceWorkbook = Workbooks.Open(Filename:="\\argsrv\Users\Sales\Unit-Dell\DELL 2022\Deliveries\Deliveries 2022.xlsx", ReadOnly:=True, Password:="", WriteResPassword:="", IgnoreReadOnlyRecommended:=True, Editable:=False, UpdateLinks:=True, CorruptLoad:=xlNormalLoad)
Set sourceWorksheet = sourceWorkbook.Worksheets("Deliveries")
'Get today's date
todayDate = Date
'Filter the source sheet based on today's date
Set filterRange = sourceWorksheet.Range("A1").CurrentRegion
filterRange.AutoFilter Field:=7, Criteria1:="=" & Format(todayDate, "dd/m/yyyy")
'Check if there are any results
If filterRange.Rows.Count <= 1 Then
MsgBox "No deliveries for today"
sourceWorkbook.Close SaveChanges:=False
Exit Sub
End If
'Copy the filtered results to the destination workbook
filterRange.SpecialCells(xlCellTypeVisible).Copy
destinationWorksheet.Range("A1").PasteSpecial xlPasteAll
'Remove the filter from the source sheet
filterRange.AutoFilter
'Close the source workbook
sourceWorkbook.Close SaveChanges:=False
End Sub
Best Regards