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:

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
35,523
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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:

Watch MrExcel Video

Forum statistics

Threads
1,109,500
Messages
5,529,222
Members
409,857
Latest member
KailuaTown
Top