Help With Updating a Global Variable from another Module

JoshuaD

Board Regular
Joined
Dec 27, 2016
Messages
54
Good Afternoon,

I am having some trouble getting something to work. The below code works flawlessly if they are all in the same module. However what I am trying to do is to have the subs for Module 2-4 be in separate modules. But still update the global variable (ShtName) in the sub DispShtName. I have tried adding the Public Shtname As String to the top of every module but it only returns blanks. The full code has over 50 different modules that are 150 rows+ so do not want to have them all on one module. Any help would be more than appreciated!

Code:
Public ShtName As String

Sub DispShtname()
Call Module2
MsgBox (ShtName)
Call Module3
MsgBox (ShtName)
Call Module4
MsgBox (ShtName)
End Sub

 '*****************************************************************
Sub Module2()
ShtName = Sheets("Contact List").Range("A5").Value
 

End Sub

 '*****************************************************************
Sub Module3()
ShtName = Sheets("Contact List").Range("A6").Value
 

End Sub

 '*****************************************************************
Sub Module4()
ShtName = Sheets("Contact List").Range("A7").Value
 

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Is that code in a standard module?
Or is it in a Sheet, ThisWorkbook, or Userform module?
 
Upvote 0
Are your macros actually called Module2 etc?
 
Upvote 0
Are your macros actually called Module2 etc?

Yes I just made something similar real fast as an example. Sorry for the confusion, they would normally be Row1, Row2, Etc... I have a lot more lines In the real modules but there will be a line in each module to update the global variable. I hope the below will clarify!

Module1:
Code:
Public ShtName As String

Sub DispShtname()

Call Row1()
MsgBox (ShtName)

Call Row2()
MsgBox (ShtName)

Call Row3()
MsgBox (ShtName)

End Sub


Module2:
Code:
Public ShtName As String

Sub Row1()

ShtName = Sheets("Contact List").Range("A5").Value
 

End Sub


Module3:
Code:
Public ShtName As String

Sub Row2()

ShtName = Sheets("Contact List").Range("A6").Value
 

End Sub


Module4:
Code:
Public ShtName As String

Sub Row3()

ShtName = Sheets("Contact List").Range("A7").Value
 

End Sub
 
Upvote 0
Remove this
Code:
Public ShtName As String
From all but one module & that should work.
It wouldn't work with the macros called Moule2 etc as VBA doesn't know if you are referring to the module or a macro.
It's always best to avoid using VBA keywords either as the name of a macro, or a variable, because VBA can get "confused", causing errors or unexpected results.
 
Upvote 0
Remove this
Code:
Public ShtName As String
From all but one module & that should work.
It wouldn't work with the macros called Moule2 etc as VBA doesn't know if you are referring to the module or a macro.
It's always best to avoid using VBA keywords either as the name of a macro, or a variable, because VBA can get "confused", causing errors or unexpected results.


Thank you! I will give it a try.
 
Upvote 0
Thank you for your help I have it working now! Turns out I just needed to add it to the "ThisWorkbook" Module and the one standard module that called the others and it is working!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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