Using the same variables for multiple macros

OldManDemosthenes

New Member
Joined
Apr 19, 2011
Messages
38
I have a macro (macro1) which sets up several variables. Is it possible to have macro2 call and use the variables setup in macro1?

So the Code for macro1 would look something like this
Code:
Sub Macro1()
Dim ALocation As Range, ARow As Long
[INDENT]Set ALocation = Worksheets("Test").Range("A:B").Find("String")[/INDENT]
[INDENT][B]ARow[/B] = ALocation.Row[/INDENT]
End Sub

and macro2
Code:
Sub Macro2()

Call Macro1
Worksheets("Test").Range("C1") = Worksheets("Test").Cells([B]ARow[/B], 10).value

End Sub

The problem I am having is that Macro2 is not recognizing the dim variables (ARow and others) from Macro1

Thanks!
 
Last edited:
I'm not sure I follow what you want, but here are a couple of ideas...

You could declare a Public array variable

Code:
Public ARow() As Variant

Sub Macro1()
    
    ARow = Sheets("Sheet2").Range("A1:B60").Value
    UserForm1.Show
    
End Sub

In this example...
ARow(10,1) would be the value in Sheet2 A10
ARow(12,2) would be the value in Sheet2 B12

Also, the UserForm procedures can read cell values just like Macro procedures. I don't follow why you need to pass 60 cell values to a UserForm.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Why not use the userform to allow the user to set which row the category each item is found in?

Or even have code that let's the user enter in the category and then locates the row, and perhaps worksheet, the category is in?
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,912
Members
452,949
Latest member
beartooth91

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