ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA code for if no visible cells after filter goto next stage (https://www.excelbanter.com/excel-programming/420827-vba-code-if-no-visible-cells-after-filter-goto-next-stage.html)

thomsonpa

VBA code for if no visible cells after filter goto next stage
 
I an running filters in VBA then copying the results to another worksheet on
the next available line. I have a challenge, that when there is nothing to
copy (i.e. the filter results are blank), I cannot get the VBA to go to the
next stage.

I have tried

Selection.AutoFilter Field:=19, Criteria1:="<"

Sheets("IMPORT").Activate
Sheets("IMPORT").Range("B2:B120").Select

If Selection.SpecialCells(xlCellTypeVisible) Then

'do something

else

'do something else

end if

But this results in an error.

Any help please.


Mike H

VBA code for if no visible cells after filter goto next stage
 
Hi,

If you try and select visible cells in a filtered range and there are none
an error is generated and you can use this

With Sheets("IMPORT")
Selection.AutoFilter Field:=19, Criteria1:="<"
On Error GoTo enditall
Set rng = .AutoFilter.Range.Offset(1,
0).Resize(.AutoFilter.Range.Rows.Count - 1, 1) _
..SpecialCells(xlCellTypeVisible)
End With
'rng exists
'Do things
enditall:
'no visible cells
'do something else

"thomsonpa" wrote:

I an running filters in VBA then copying the results to another worksheet on
the next available line. I have a challenge, that when there is nothing to
copy (i.e. the filter results are blank), I cannot get the VBA to go to the
next stage.

I have tried

Selection.AutoFilter Field:=19, Criteria1:="<"

Sheets("IMPORT").Activate
Sheets("IMPORT").Range("B2:B120").Select

If Selection.SpecialCells(xlCellTypeVisible) Then

'do something

else

'do something else

end if

But this results in an error.

Any help please.


thomsonpa

VBA code for if no visible cells after filter goto next stage
 
Looks like this will work, thank you very much. The enditall was what I was
looking for, I will use this again in future.
I don't know what I would do if it wasn't for this discussion group. It
helps me with so much.

Thank you, and keep up the good work.

"Mike H" wrote:

Hi,

If you try and select visible cells in a filtered range and there are none
an error is generated and you can use this

With Sheets("IMPORT")
Selection.AutoFilter Field:=19, Criteria1:="<"
On Error GoTo enditall
Set rng = .AutoFilter.Range.Offset(1,
0).Resize(.AutoFilter.Range.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
End With
'rng exists
'Do things
enditall:
'no visible cells
'do something else

"thomsonpa" wrote:

I an running filters in VBA then copying the results to another worksheet on
the next available line. I have a challenge, that when there is nothing to
copy (i.e. the filter results are blank), I cannot get the VBA to go to the
next stage.

I have tried

Selection.AutoFilter Field:=19, Criteria1:="<"

Sheets("IMPORT").Activate
Sheets("IMPORT").Range("B2:B120").Select

If Selection.SpecialCells(xlCellTypeVisible) Then

'do something

else

'do something else

end if

But this results in an error.

Any help please.



All times are GMT +1. The time now is 06:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com