using variables from other macro

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
How can I make the second marco print out the values from the first macro?
for example I want to use the second marco to msg out x and y which are variable in first macro. Thank you so much.

Code:
Sub myinputbox()
    'to use msgbox
    Dim x As Integer
    Dim y As String
    x = InputBox("enter a number")
    y = InputBox("enter a name")
    
End Sub


Sub mymsgbox()
    MsgBox (x)
    MsgBox (y)
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this

Code:
Sub myinputbox()
    'to use msgbox
    Dim x As Integer
    Dim y As String
    x = InputBox("enter a number")
    y = InputBox("enter a name")
    Call mymsgbox(x, y)
End Sub


Sub mymsgbox(x, y)
    MsgBox (x)
    MsgBox (y)
End Sub
 
Upvote 0
Or use global variables:

Code:
Global x As Integer
Global y As String


Sub myinputbox()
    'to use msgbox
    x = InputBox("enter a number")
    y = InputBox("enter a name")
    
End Sub

Sub mymsgbox()
    MsgBox (x)
    MsgBox (y)
End Sub
 
Upvote 0
Or use global variables:

Code:
Global x As Integer
Global y As String


Sub myinputbox()
    'to use msgbox
    x = InputBox("enter a number")
    y = InputBox("enter a name")
    
End Sub

Sub mymsgbox()
    MsgBox (x)
    MsgBox (y)
End Sub
Two notes about Eric's suggestion...

1) I would suggest using complex variable names that cannot easily be called accidentally from within some other unintended procedure.

2) You cannot Dim those global variables within any procedure that will make use of them otherwise they will not be global to that procedure.
 
Upvote 0
Two notes about Eric's suggestion...

1) I would suggest using complex variable names that cannot easily be called accidentally from within some other unintended procedure.

2) You cannot Dim those global variables within any procedure that will make use of them otherwise they will not be global to that procedure.
Both good points. Modern program design pretty much states that global variables should be used exceedingly rarely. Variables should be defined locally to the function or procedure, and communication between procedures should be via parameters in the Call statement, like Dante suggested. This allows each procedure to be treated as a "Black box", and you can code and test it independently of the other modules. That said, there are some legitimate times you could use a global variable.

Also as another way to handle your example, you could write the x and y variables to a location in the spreadsheet, and the second procedure could read them.
 
Upvote 0
Thanks all for the help. I did the Global x and y but did not work.
each macro would start with a line ------- and end with a line ----
when I start typing "Global x As Integer" and press enter
excel would move "Global x As Integer" above the line of the macro, like this


Global x As Integer
Global y As String
____________________________________________
Sub myinputbox()
'to use msgbox
x = InputBox("enter a number")
y = InputBox("enter a name")
End Sub
_____________________________________________
Sub mymsgbox()
MsgBox (x)
MsgBox (y)
End Sub
 
Upvote 0
That's normal. Excel is showing that the global variables do not belong to either routine, but rather to all of them. Did you actually try to run your code?
 
Upvote 0
It is completely normal.
It should look like this:


6ba3c159271b6cb03c893a6594e005be.jpg




By the way, the declaration of the variable x is not correct, since the inputbox returns a text, then if you capture a letter it will send you an error.
You should declare as string and then convert the value to a numerical value.


Finally, you need this for something specific or just as general knowledge. Because if it's for something specific, maybe we can help you with that problem.
 
Upvote 0
Thanks all again for the help. I did run the code above but msgbox was empty in both cases. I entered x=10 and y="hello". Thanks a lot
 
Upvote 0
First you must run this macro: inputbox and then this macro: mymsgbox
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,812
Members
449,048
Latest member
greyangel23

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