Continue to the next item in a for each loop if auofilter returns no results for current object in collection

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a with statement nested within a for each loop. What is the syntax to continue on to the next item in the for each loop from within the with statement. I have tried to insert the continue statement after checking the count of the visible rows=1 (the header) but that doesn't work.

My code is

VBA Code:
        For Each ws In wb2.Worksheets
                If ws.Name <> "Cancellations" And ws.Name <> "Totals" And ws.Name <> "Sheet2" Then
                        With ws.[A3].CurrentRegion
                                'On Error Resume Next
                                'Autofilter the late cancel date enter in B34 with dates in column 1
                                .AutoFilter 1, LCDt
                                'Autofilter the late cancel request number with request numbers in column 3
                                .AutoFilter 3, LCReq
                                'Add the service to a varaible
                                'Service = .Areas(1).Cells(2, 5).Value
                                
                                
                                
                                'Add code for filter to check number of
                                AutoFilterCounter = .Columns(1).SpecialCells(xlCellTypeVisible).Count
                                    'If value is 1, only the heading is visible so skip to the next sheet.
                                    If AutoFilterCounter = 1 Then
                                        Continue
                                
                                    
                                    
                                    With Application.Intersect(.SpecialCells(xlCellTypeVisible), .Offset(1, 0))
                                    
                                        Service = .Areas(1).Cells(2, 5).Value
                                    End With

                                With Data
                                    .Cells(30, 1) = LCDt
                                    .Cells(30, 2) = Service
                                    .Cells(30, 5) = 3
                                    .Cells(30, 6) = 1
                                End With
                                
                                LCPrice = Data.Cells(30, 8).Value
                                
                                    'With Application.Intersect(.SpecialCells(xlCellTypeVisible), .Offset(1, 0))
                                        .Areas(1).Cells(2, 8).Value = LCPrice
                                        .Areas(1).Cells(2, 9).Formula = "=IF(RC[-4]=""Activities"",0,RC[-1]*0.1)"
                                        .Areas(1).Cells(2, 10).Formula = "=RC[-1]+RC[-2]"
                                    'End With
                                
                              
                                .AutoFilter
                        End With
                End If
        Next ws
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Instead of trying to skip if AutoFilterCounter = 1, just try to do what you want if AutoFilterCounter > 1

That is something like this.

VBA Code:
With ws.[A3].CurrentRegion

  'Some code here
  
  If AutoFilterCounter > 1 Then

    'All the rest of what you want to happen if visible rows > 1
  
  End If
End With
.
 
Upvote 0
Haven't had a chance to try it yet but I was wondering, would it be better to have an if or a while?
 
Upvote 0

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

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