Thread: Scope of Subs?
View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones Norman Jones is offline
external usenet poster
 
Posts: 5,302
Default Scope of Subs?

Hi Ken,

The primary distinction here is that the ThisWorkbook, Sheet and Userform
modules are Class modules and a call to a procedure in a class module must
include the module name.


---
Regards,
Norman



"Ken Loomis" wrote in message
...
So, as I look over my VBA project, I notice that I call a module sub from
a workbook sub without any special designation.

I assume that since this works that means that all the modules are 'under'
or 'inside of' the workbook, thus all the module subs are within the scope
of the workbook and accessible from the workbook.

But since the workbook is a higher order object from the modules, I have
to use the "ThisWorkbook" designation to access a workbook sub from a
module.

I am just trying to get a grip on this object model stuff.

Thanks for any clarification you can provide.

Ken


"Norman Jones" wrote in message
...
Hi Ken,

Look again at my example and change:

ProjectExpired


to:

ThisWorkbook.ProjectExpired


---
Regards,
Norman



"Ken Loomis" wrote in message
...
I tried that.

Here is the workbook sub I am trying to call from a module:

Public Sub ProjectExpired()
Dim ErrorMessage As StreetLeads

ErrorMessage = "This workbook has expired." & vbNewLine
ErrorMessage = ErrorMessage & "Please contact the programmer for a
new version." & vbNewLine
MsgBox (ErrorMessage)
NewDeleteAllCode
mySave = True
ActiveWorkbook.Save
mySave = False
End Sub

Here is the call to that sub:

If IITTDH ExpTest Then
ProjectExpired
Exit Sub
End If

When I compile the project, I get a Compile error: Sub or function not
defined with ProjectExpired highlighted in the module code.

Any suggestions?

Thanks,
Ken



"Norman Jones" wrote in message
...
Hi Ken,

You can call a sub in the Thisworkbook module if the sub is declared
public.

For example:

In the ThisWorkbook module:
'-------------------------------
Public Sub Workbook_Open()
MsgBox "Hello from Workbook_Open!"
End Sub

In a standard module:
---------------------
Sub CallIt()
ThisWorkbook.Workbook_Open
End Sub


---
Regards,
Norman



"Ken Loomis" wrote in message
...
I am trying to call a sub in the workbook object from a sub in a
module, but I can't seem to make that work.

Is there a way to make that work?

TIA,
Ken