Use variable from another sub

kash04kk

New Member
Joined
Oct 15, 2018
Messages
1
Hello everyone,

I believe this would be a very easy question for the experts here. I want to use variables from another sub/module. An example of the code in module 1 is below:
Code:
Public pinit, uy_from, uy_to As Variant, ws As Worksheet


Sub TC()


pinit = InputBox("initials")


uy_from = InputBox("Year From")


uy_to = InputBox("Year To")

........ all the code is put here

End Sub

An example of code in module 2 is below:
Code:
Sub Analysis()


Workbooks(pinit & "_Data_Compiled_UY_" & uy_from & "_" & uy_to & ".xlsx").Activate



.............. all the code is here

End Sub

The problem is the variable pinit, uy_from and uy_to are not recognized in module 2 sub. I tried to declare the variables as public (as shown above) but still, it is not working. I would appreciate the help from the forum

Thanks
 
Last edited by a moderator:

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).
Welcome to the forum.

That should work, based on your description, assuming you ran TC first and then didn't do anything that would cause state loss (eg an unhandled error in your code, or resetting the VBE). Also assuming you haven't declared the variables again elsewhere.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,927
Members
448,533
Latest member
thietbibeboiwasaco

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