Assigning a value to a variable

hallingh

Well-known Member
Joined
Sep 11, 2010
Messages
769
I have some macro code which does a number of things, but for one sections I am calling a Sub from another module. Both the Sub in the other module and the main macro code contain the variable r dimmed as long. I am assigning a value to r in the main macro code, and I want that value to be assigned to r in the sub in the other module as well.

I have the code written like this:

Code:
Sub MainMacro()
Dim r As Long
Dim x As Long
 
r = 8
Do While r < 250
Call SubFromOtherModule
r = r + 8
Loop
 
r = 258
Do While r < 498
Call SubFromOtherModule
r = r + 8
Loop
 
End Sub

And I have the Sub in the other module written like this:

Code:
Sub SubFromOtherModule()
Dim r As Long
Dim x As Long
 
Run Code
End Sub

I know this isn't written right, but I want the SubFromOtherValue to automatically take the value for r and x from the main body of code.

If anyone could help me out with this I would be greatly appreciative. Thanks for the looks!

Hank
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try like this - be sure to delete Dim r as Long from SubFromOtherModule

Code:
Sub MainMacro()
Dim r As Long
Dim x As Long
 
r = 8
Do While r < 250
Call SubFromOtherModule(r)
r = r + 8
Loop
 
r = 258
Do While r < 498
Call SubFromOtherModule(r)
r = r + 8
Loop
 
End Sub


Sub SubFromOtherModule(r As Long)
Dim x As Long
 
Run Code
End Sub
 
Upvote 0
I think you need to Public r.
At the top of the module, before the macro, enter
Code:
Public r as Long
the remove the "Dim r as Long" statements

lenze
 
Upvote 0

Forum statistics

Threads
1,224,509
Messages
6,179,194
Members
452,893
Latest member
denay

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