If Statement Failing

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,566
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Consider the code below.
Worksheet 'ws_sand' is filtered (column A) to include all dates between two date ranges (f_lr & f_ur).
Rows 1 and 2 are static header rows, Row 2 holds the column (field) names.
For the most part this is working, except when there are no dates found as a result of the filter. There is no data displayed below row 2.
The line purple is meant to identify this scenario, but it is not. In my testing, when there are no rows returned as part of the filter, I am NOT getting the msgbox displayed and the nfm counter is not increased.

What must I do to improve my code to have this work as desired?


Rich (BB code):
With ws_sand
        .AutoFilterMode = False 'turn off autofilter if on
        'f_lr = ws_sheet2.Cells(i, 11)
        'f_ur = ws_sheet2.Cells(i, 12)
        last_col = .Range("A1").CurrentRegion.Columns.Count
        Set f_range = .Range(.Cells(2, 1), .Cells(2, last_col))
        With f_range
            .AutoFilter field:=1, Criteria1:=">=" & f_lr, Operator:=xlAnd, Criteria2:="<=" & f_ur
        End With
        With ws_sand
            If .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row = 1 Then
                MsgBox "No sand records.", vbInformation, "NO SAND USED"
                nfm = nfm + 1
            End If
        End With
End With
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
VBA Code:
If .Range("A1", .Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).CountLarge = 2 Then
 
Upvote 0
When I deal with the same type of thing working with tables I use something like

VBA Code:
With .ListObjects("tblTemplate").DataBodyRange

     x3 = .SpecialCells(12).Rows.Count
     If Err.Number <> 0 Then
     Msgbox ("error etc.")
 
Upvote 0
VBA Code:
With ws_sand
            If .Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Row = 1 Then

Note that It seems you are looking at your headers with this code, which would always be visible and the top row will always be 1. As per other suggestions you would need to be doing more than just checking the first row ...

i.e, Fluff's solution which is checking if only two rows are visible on the worksheet looks fine.
 
Upvote 0
Thank you all for your suggestions.
"countlarge" is something new to me. I'll have to look deeper into that.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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