Msg box in a destination xlsm if no results are found in source xlsx

Gtasios4

Board Regular
Joined
Apr 21, 2022
Messages
80
Office Version
  1. 2021
Platform
  1. Windows
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

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
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Gtasios4,

with

VBA Code:
    Set filterRange = sourceWorksheet.Range("A1").CurrentRegion

you build a range holding the entire rows and coloumns directly connected to A1 and by using

VBA Code:
    If filterRange.Rows.Count <= 1 Then

you check the entire range no matter if rows are hidden - the result will always be the number of rows for the basic filterRange..

Try using

VBA Code:
    If filterRange.SpecialCells(xlCellTypeVisible).Rows.Count <= 1 Then

or

VBA Code:
    If sourceWorkbook.Range("A" & sourceWorkbook.Rows.Count).End(xlUp).Row = 1 Then

instead.

Ciao,
Holger
 
Upvote 0
Hi Gtasios4,

with

VBA Code:
    Set filterRange = sourceWorksheet.Range("A1").CurrentRegion

you build a range holding the entire rows and coloumns directly connected to A1 and by using

VBA Code:
    If filterRange.Rows.Count <= 1 Then

you check the entire range no matter if rows are hidden - the result will always be the number of rows for the basic filterRange..

Try using

VBA Code:
    If filterRange.SpecialCells(xlCellTypeVisible).Rows.Count <= 1 Then

or

VBA Code:
    If sourceWorkbook.Range("A" & sourceWorkbook.Rows.Count).End(xlUp).Row = 1 Then

instead.

Ciao,
Holger
Hi Holger,

Thank you for your kind reply. I've replaced in the code with the one's you suggest as followed but it doesn't operates as I want. Instead it makes visible the source xlsx filters the results and also gives the msg box

VBA Code:
   'Check if there are any results
        If filterRange.SpecialCells(xlCellTypeVisible).Rows.Count <= 1 Then
        MsgBox "No deliveries for today"
        sourceWorkbook.Close SaveChanges:=False
        Exit Sub
    End If

To briefly describe, I have an xlsx source data with deliveries in rows and there is also a column with the date.

My point is with the above mentioned code to copy the today's deliveries and then pasting into the destination xlsm. If no today's deliveries are found under the headers of the source file then pop a msg box "no deliveries for today"
 
Upvote 0
Hi Gtasios4,

please give this updated code a try (I had some trouble with CLng(Date) as well as CDbl(Date) being accepted as criteria to return data for today):

VBA Code:
Sub FilterTableByDate()
' https://www.mrexcel.com/board/threads/msg-box-in-a-destination-xlsm-if-no-results-are-found-in-source-xlsx.1230257/
    Dim sourceWorkbook As Workbook
    Dim destinationWorkbook As Workbook
    Dim sourceWorksheet As Worksheet
    Dim destinationWorksheet As Worksheet
    Dim sourceTable As ListObject
    Dim destinationTable As ListObject
    '/// changed Type from Date to String for next Variable
    Dim todayDate As String
    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
    '/// changed to build the Date in a way that the filter for Today was successfull
    todayDate = Format(Date, "m\/d\/yyyy")
    
    'Filter the source sheet based on today's date
    Set filterRange = sourceWorksheet.Range("A1").CurrentRegion
    '/// changed the criteria for AutoFilter as in my workbook the conversion of Date with CLng or CDbl for AutoFilter did not work
    filterRange.AutoFilter Field:=7, _
                            Operator:=xlFilterValues, _
                            Criteria2:=Array(2, todayDate)
    
    'Check if there are any results
    '/// used checking for the last visible row on the sheet to determine if there are records
    If sourceWorkbook.Range("A" & sourceWorkbook.Rows.Count).End(xlUp).Row < 2 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

Regarding Instead it makes visible the source xlsx filters: AFAIK that is the custom way for applying AutoFilter, you would need to set the Visibledropdown for each column to False in order not to show them.

Holger
 
Upvote 0
Solution

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

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