Refering to code in worksheet

dugdugdug

Active Member
Joined
May 11, 2012
Messages
342
Sheet1 is named Graph and contains some code:

Code:
Sub MyWorksheetCode

' just some code

End Sub

In Module1, I have the following:

Code:
Sub MyModuleCode

Dim wks as Worksheet

Set wks = Worksheets("Graph")

' Call wks.MyWorksheetCode <- This does not work

Call Sheet1.MyWorksheetCode

End Sub

Why is it in the above, calling the subroutine using the variable wks does not work?

Thanks
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,682
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Because the Worksheet class (from which Sheet1 inherits) does not have a MyWorksheetCode method.
 
Upvote 0

nuked

Well-known Member
Joined
Mar 20, 2013
Messages
883
Try wrapping your worksheet code in a module and call it from the worksheet rather than the other way round.

Module code:

Sub DoSomethingWithAChart(obj as Variant)

End Sub


Worksheet code:

DoSomethingWithAChart(me.Chart1)
 
Upvote 0

Sixthsense

Active Member
Joined
Nov 19, 2012
Messages
385
ADVERTISEMENT
Or don't declare it........

Code:
Sub MyModuleCode

Set wks = Worksheets("Graph")
Call wks.MyWorksheetCode

 End Sub
 
Upvote 0

dugdugdug

Active Member
Joined
May 11, 2012
Messages
342
Because the Worksheet class (from which Sheet1 inherits) does not have a MyWorksheetCode method.

Sorry if I'm being ignorant but

Code:
Sub MyWorksheetCode()
' just some code
End Sub

is in Sheet1.

Try:

Code:
Application.Run wks.CodeName & ".MyWorksheetCode"

Afraid it didn't work. (BTW, what is CodeName)?

Or don't declare it........

Code:
Sub MyModuleCode

Set wks = Worksheets("Graph")
Call wks.MyWorksheetCode

 End Sub

Not declaring doesn't even compile.

However, this works:

Code:
Sub MyModuleCode()
Dim wks
Set wks = Worksheets("Graph")
Call wks.MyWorksheetCode
End Sub

So it seems it doesn't like it if wks is declared as a Worksheet.
 
Last edited:
Upvote 0

Sixthsense

Active Member
Joined
Nov 19, 2012
Messages
385
ADVERTISEMENT
But for me its working fine :)
 
Upvote 0

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,682
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
So it seems it doesn't like it if wks is declared as a Worksheet.

That's my point. The generic Worksheet class does not have a MyWorksheetCode method. Your specific Sheet1 class, which inherits from the generic class, does have that method. You can declare the variable as a Variant (as you have) or an Object or as Sheet1:
Code:
Sub MyModuleCode()
' any of these will work
Dim wks as Sheet1
' Dim wks as Object
' Dim wks

Set wks = Worksheets("Graph")
Call wks.MyWorksheetCode
End Sub
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
You have other answers, but the code I posted worked for me:

Code:
'Sheet1 module
Sub MyWorksheetCode()
    MsgBox "Hello"
End Sub

'General module
Sub MyModuleCode()
    Dim wks As Worksheet
    Set wks = Worksheets("Graph")
    Application.Run wks.CodeName & ".MyWorksheetCode"
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,829
Messages
6,011,838
Members
441,650
Latest member
ceyoung75

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
Top