Workbook_SheetChange event loses reference to Workbook

Tim_Excel_

Well-known Member
Joined
Jul 12, 2016
Messages
512
Hi forum,

I have a Workbook_SheetChange event for another workbook (other than the workbook that holds the code). At the end of a lengthy Sub, I call for the Sub that defines the Workbook's name that the event needs to work for:

Code:
Sub step1()

'''''code''''

NameGrabber.DefineWorkbookName

End Sub

Code:
Dim oWb As New Updater

Sub DefineWorkbookName()
Application.EnableEvents = True
Dim AddNew As Workbook
    Set AddNew = Workbooks("test1")
    Set oWb.Workbook = AddNew


End Sub

Then, in a Class Module sits the Workbook_Sheetchange event:
Code:
Public WithEvents m_wb As Workbook

Public Property Set Workbook(wb As Workbook)
    Set m_wb = wb
End Property


Public Property Get Workbook() As Workbook
    Set Workbook = m_wb
End Property

Public Sub m_wb_SheetChange(ByVal Sh As Object, ByVal Target As Range)

''''code''''

End Sub

Problem is, the SheetChange event will only run if I manually run the DefineWorkbookName Sub myself first, even though the code is set to run (and does run) through this sub already.

Is there any way I could have the SheetChange event run without having to run the DefineWorkbookName sub manually first? Thanks a lot!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
even though the code is set to run (and does run) through this sub already

Not sure what you mean by that? If you don't run the code to set up the listener, nothing will be listening for that event.
 
Upvote 0
Rory,

Since I call the DefineWorkbookName Sub, the Workbook name is defined for the WorksheetChange event. What I am trying to say is that this sub already runs in my code, but I STILL have to run it a second time manually because otherwise the WorksheetChange simply doesn't run.
 
Upvote 0
Then I suspect something in your code is causing a state loss and resetting your oWb variable.
 
Upvote 0
What would you suspect is causing this loss? I don't see the cause, the DefineWorkbookName Sub runs on the very end and then only the WorksheetChange event is supposed to run. There is no code that runs between setting the oWb variable and running the WorksheetChange event.
 
Upvote 0
In the absence of any code to go on at all, I'll take a wild stab at you adding activex controls to a worksheet.
 
Upvote 0
Rory,

There is an ActiveX button on the workbook that holds the code. This button is used to start the sep1 Sub that ends in the DefineWorkbookName sub being called. Could I maybe write the workbook name in a cell somewhere and then refer to that cell in the WorksheetChange event?
 
Upvote 0
The button would only be an issue if you created it in the code. Writing the name won't help if you don't have a listener responding to the event. Without any code to look at I can't really assist you further.
 
Upvote 0
Rory, perhaps you could educate me on what a listener is? I am very much a novice when it comes to this kind of code, I have pulled the code for the WorksheetChange event from another forum.

I could share with you the code but I assure you it would be to no assistance to solving this problem. None of the code not shown in my primary post has anything to do with the WorksheetChange event, and the code in the event itself does not even run like I mentioned.
 
Upvote 0
Your Updater class is the listener - it has declared a WithEvents variable, so it sits there "listening" for that event being raised, then does its thing.

I could share with you the code but I assure you it would be to no assistance to solving this problem.

How do you know that? You say that the code does work if you run that routine yourself, but doesn't when it's run from your code. That would tend to imply that the problem is with the code you are not posting, would it not? ;)
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,314
Members
449,081
Latest member
tanurai

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