VBA Script fails when triggered by Worksheet_Activate()

alexthecamel

New Member
Joined
Jul 6, 2004
Messages
47
Hi All,

Quite embarrassed with this one but can't for the life of me fix it.

I have a VBA script that works perfectly when triggered as a Macro, but fails when triggered by Worksheet_Activate.

The line that fails is:

dest.Range("A6:M1000").AutoFilter Field:=pn.Column, Criteria1:=""

Where 'dest' is a previously defined Worksheet and pn is previously defined Range (single cell reference).

The error I get is:

"method 'Find' of object 'Range' failed"

Any gentle kicks in the right direction would be much apprecaited.

Thanks in advance
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Which line is highlighted if you click Debug when you get the error?

Is that all the code?

Do you have any other code that could be triggered by this code?
 
Upvote 0
Thanks for replying!

Apologies copied the wrong line the highlighted line is: Set pn = dest.Range("A1:M1").Find(sName, lookat:=xlPart)
I suspect it's due to my use of dest.Range

more fully the script is (had to redact some bits)

Dim dest As Worksheet
Set dest = ActiveWorkbook.ActiveSheet

Dim source As Worksheet
Set source = ActiveWorkbook.Worksheets("SourceSheetName")

Dim sName As String
sName = ActiveWorkbook.ActiveSheet.Name

Set pn = dest.Range("A1:M1").Find(sName, lookat:=xlPart)

.... loads more code

I can bypass that part but it then fails at the line originally posted, which also uses dest.Range so seems likely somehow related to that, but I can't work out why it works as a manualy triggered Macro but not on Worksheet_Activate()

Thanks again.
 
Upvote 0
In that part of the code are you only trying to find which column sName is in?
 
Upvote 0
Yes, ultimately I'm looking in that range to find the column sName is in, then will be Autofiltering to just displays blanks in that column and deleting them.
 
Upvote 0
As you're using the activate event, how about
Code:
Private Sub Worksheet_Activate()
Dim Pn As Range
Dim source As Worksheet

Set source = ActiveWorkbook.Worksheets("SourceSheetName")

Set Pn = Me.Range("A1:M1").Find(Me.Name, , , xlPart, , , False, , False)
Me.Range("A6:M1000").AutoFilter Field:=Pn.Column, Criteria1:=""
End Sub
 
Upvote 0
Thanks Fluff,

Only tested in issolation so will need to integrate with the rest of my code, but certainly does what I asked, thanks for the help.
First time I've come accross this use of 'Me', handy trick.

Thanks also to Norie for replying, as ever the help is very much appreciated.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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