Variables not available

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
539
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have one module with three separate sub-routines like so:
Module1
-->Macro1
-->Macro2
-->Macro3

I call Macro1 from a command button on one of my sheet tabs. It starts by running Macro2, then Macro3 before running through it's own code. There are two strings from the secondary macros I need in order for Macro1 to run properly but neither is "available" after they finish and Macro1 resumes running.

I have the following two lines at the top of the module:
Public macro2Str As String
Public macro3Str As String

What else do I need to change or add?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Maybe all you need to do is declare your variables as Public at the top of a standard module, such as where macros and UDFs go, and not in the worksheet module.
 
Upvote 0
Omit any "Dim macro2Str as String" from Macro2
Omit any "Dim macro3Str as String" from Macro3

If the sole purpose of Macro2 and Macro3 is to figure out the respective string variables, possibly consider making them functions.
i.e.
Code:
Sub Macro1
  Macro1Str2 = fnMacro2()
End Sub
 
Function fnMacro2()
  fnMacro2 = macro2Str 
End Function
Then you should not need the public variables.
 
Upvote 0
Tom:
None of these are at the worksheet level. I got into the practice of adding each macro to it's own module just so I could view everything at a glance. Then I would declare public variables when calling the secondary macros (each in separate modules) which has always worked fine and I never had a problem until this project. I decided to group similar macros within the same module to help keep things organized as the scope of this project is much larger than most of my previous projects. I don't understand why it works any different.

Tweedle:
I have not declared any of the public variables within the secondary macros, but that is the sole purpose of the secondary macros. What's the difference between a macro and a function and is there anything else I would need to change in the code? Also, does this line of code serve the same purpose as the "call" statement:
Macro1Str2 = fnMacro2()
 
Last edited:
Upvote 0
How many public variables are you using in all?

Are you sure you actually need to use them?

They can be useful but if not handled properly problems can creep in to the code.

Have you considered passing arguments to your subs, or are you already doing that?
 
Upvote 0
I have not declared any of the public variables within the secondary macros...
I had the two public variables declared in the main macro which was causing the problem and didn't notice until just now.
 
Upvote 0
Code:
Sub FOOB()
'This ...is a Sub
Dim retval As String
retval = HW("dlroW olleH") 'The Function Returns whatever it's purpose is
'Note: No need for Public Variable
MsgBox retval
End Sub
 
Function HW(varIN As String)
'Function Return any Calculations to the Function Name
'which is passed back to the calling module
HW = StrReverse(varIN)
End Function

Also, does this line of code serve the same purpose as the "call" statement:
Macro1Str2 = fnMacro2()
In terms of a Function returning some value, I suppose.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

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