Variables between modules

Lavina

Board Regular
Joined
Dec 18, 2018
Messages
75
Hello guys,

I've been having trouble getting global variables to work.

Module1 code:
Code:
Option Explicit Public Test As Variant
Sub init()
test = 2
Call testing
End Sub
Module2 code:
Code:
Option Explicit Sub Testing()
test = 3
End Sub
All i want is to throw over my test variable value and update it. But once i reach module2, no value exists for test and once done the value of test = 2

Global instead of public yields the same result

My desired result is, test = 3. What am i doing wrong
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Is the line "Public test as Variant" at the very top of your code in Module 1? Before any other subs? It can be after "Option Explicit" but there does need to be a space... not sure if it's just the formatting the way you pasted it into the forum but double-check that. Also did you try writing "Public Sub" instead of just "Sub" ?
 
Upvote 0
That code should work quite happily, what makes you think it isn't?
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,575
Members
449,039
Latest member
Arbind kumar

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