Pass variables from sub to another sub

dakke

New Member
Joined
Oct 29, 2017
Messages
5
Hi all,

So far I love tweaking it all and taking my first steps in Excell. I've been looking around, but I can't seem to find a simple solution.

What I would like to do now is to pass variables from one sub to another (all in the same module). The idea is simple. I have a sub, where I call 2 other subs. In the second, I have a simple string I want work with, in the third sub there is a calculation I need. Here's a short example of what I try to do in code (with presumable a lot of mistakes).:
Code:
Sub hallo()
k As Integer

k = 7

Call Message
Call calculations

MsgBox text & " - " & calc

End Sub


Sub calculations(k As Integer)

calc = k * 5

End Sub


Sub Message()
text As String

text = "It worked!"

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
something like declare as private above the first module then you should be able to use it as a global value
 
Upvote 0
You need the variables declared above the code, you also don't need Call.

Code:

Code:
Dim k As Integer
Dim text As String
Dim calc As Long


Sub hallo()

k = 7

Message
calculations

MsgBox text & " - " & calc

End Sub


Sub calculations()

calc = k * 5

End Sub


Sub Message()

text = "It worked!"

End Sub
 
Last edited:
Upvote 0
Another option would be to use a structure like this
Code:
Sub hallo()
  Dim k As Integer
  
  k = 7
  MsgBox Message & " - " & calc(k)
End Sub

Function calc(myInput As Integer) As Integer
  calc = myInput * 5
End Function

Function Message() As String
  Message = "It worked!"
End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

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