ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Scope of Subs? (https://www.excelbanter.com/excel-programming/336569-scope-subs.html)

Ken Loomis

Scope of Subs?
 
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



Norman Jones

Scope of Subs?
 
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




Norman Jones

Scope of Subs?
 
Hi Ken,

Just to add, procedures in worksheet modules can similarly be called from a
standard sub, providing the worksheet procedure is declared Public.

E.g.:

Public Sub Worksheet_Calculate()
MsgBox "Hi from " & Me.Name & "Calculate"
End Sub

Sub CallIt2()
Sheet1.Worksheet_Calculate
End Sub


Note that ThisWorkbook and Sheet moule procedures are Private by default.


---
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




Ken Loomis

Scope of Subs?
 
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






Ken Loomis

Scope of Subs?
 
Just a note that this line of code:

Dim ErrorMessage As StreetLeads

Should have been:

Dim ErrorMessage As String

And even when I fix that, I get the same error message.

Ken


"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








Norman Jones

Scope of Subs?
 
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








Ken Loomis

Scope of Subs?
 
Thanks, Norman. as I am sure you're already aware, that worked.

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










Ken Loomis

Scope of Subs?
 
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










Norman Jones

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












Bob Phillips[_6_]

Scope of Subs?
 
Implicitly declared classes, which is why you don't have to New these
classes.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Norman Jones" wrote in message
...
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















All times are GMT +1. The time now is 11:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com