Workbook BeforeClose Event Error

D

dd

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Current Issues Log").Select
Selection.AutoFilter Field:=4, Criteria1:="<>Closed", Operator:=xlAnd
End Sub

I am using the above to autofilter my sheet before close. I am using this
identical code in two workbooks, which have the same worksheet names. It
works fine when I close one workbook at a time, but if I try to shut down
excel with both books open, I get a runtime error '1004' select method of
worksheet class failed.

Is there a way round this?

Regards
Dylan
 
P

papou

Hello
Change
Sheets("Current Issues Log").Select
To
Thisworkbook.Worksheets("Current Issues Log").Select

But you don't need to select your sheet, so you may consider using something
like:
Thisworkbook.Worksheets("Current Issues Log").Range("A1") _
..AutoFilter Field:=4, Criteria1:="<>Closed", Operator:=xlAnd

HTH
Cordially
Pascal
 
D

dd

Pascal

Thank you

Kind Regards
Dylan

Hello
Change
Sheets("Current Issues Log").Select
To
Thisworkbook.Worksheets("Current Issues Log").Select

But you don't need to select your sheet, so you may consider using something
like:
Thisworkbook.Worksheets("Current Issues Log").Range("A1") _
..AutoFilter Field:=4, Criteria1:="<>Closed", Operator:=xlAnd

HTH
Cordially
Pascal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Apply macro to all worksheets 15
Save BeforeClose 1
Workbook.close & beforeclose 2
Custom filter 26
how to quit in BeforeClose 3
Delete rows with different criteria 7
BeforeClose Event 1
Activate macro in another book 2

Top