How do I assign string variable values across multiple subs and modules once?

bellg

New Member
Joined
May 13, 2014
Messages
29
Scenario:
I have 3 subs inModule 1.
Each one of thesesubs uses some but not all of 5 available variables. All Strings.

Instead of pickingand choosing which variables to define in each of the 3 subs, I'd like tocreate 1 more sub in Module 2. This sub would be called at the start of each ofthe 3 subs in Module 1 with the intent of defining all variables all the time.

Currently, it showsthat the the variable was defined, but it doesn't always translate into each ofmy other subs. I have message boxes in both the sub in Module 2 and the sub inModule 1 to show the variable's value during the run. Whenever the message boxin Module 1 runs, it doesn't always give me a value and is sometime blank.

Does anyone know whymy variables won't always translate from sub4 in Module 2 to a subs1-3 inModule 1 after sub4 is called? What's weird is that it started out NOT workingfor me, and then it started working, so I added more variables, and now it'sNOT working again for those added variable. It's like there's a time limit onwhen you add it or something, which doesn't make any sense to me.

If there's a betterway to do this, please let me know!

Rich (BB code):
Rich (BB code):
'In Module 1
Public Sub one()
Call four
MsgBox (variable1)
MsgBox (variable2)
End Sub
 
Public Sub two()
Call Variables
MsgBox (variable1)
MsgBox (variable4)
End Sub
 
Public Sub three()
Call Variables
MsgBox (variable3)
MsgBox (variable5)
End Sub
 
 
'In Module 2
Public variable1 AsString
Public variable2 AsString
Public variable3 AsString
Public variable4 AsString
Public variable5 AsString
 
PublicSub four()
WithActiveWorkbook
   'selects column C first
   CurrRow = ActiveCell.Row
   Range("C" & CurrRow).Select
   variable1 = ActiveCell.Offset(0, -1).Value
   variable2 = ActiveCell.Value
   variable3 = ActiveCell.Offset(0, 1).Value
   variable4 = ActiveCell.Offset(0, 2).Value
   variable5 = ActiveCell.Offset(0, 3).Value
EndWith
EndSub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Code:
    Sub CallingProcedure()
        Dim Range1 As Range
        Dim Range2 As Range
        Set Range1 = Range("A1")
        Set Range2 = Range("A2")
        Range1.Value = 123
        Range2.Value = 456
        
        CalledProcedure R1:=Range1, R2:=Range2
    End Sub
    
    Sub CalledProcedure(ByRef R1 As Range, ByVal R2 As Range)
        R1.Value = 321
        R2.Value = 654
        
        Set R1 = Range("A3")
        Set R2 = Range("A4")
    End Sub

I found the above code from your second link. What it does is define the variables in the calling sub, then redefines them in the called sub.

I'm looking for something that only requires defining variables in the called sub to reduce redundancy. Is that possible?

Something more along the lines of this code that definitely doesn't work:
Code:
    Sub CallingProcedure()
        CalledProcedure R1:=Range1, R2:=Range2
    End Sub
    
    Sub CalledProcedure(ByRef R1 As Range, ByVal R2 As Range)
        R1.Value = 321
        R2.Value = 654
        
        Set R1 = Range("A3")
        Set R2 = Range("A4")
    End Sub
 
Upvote 0
Code:
    Sub CallingProcedure()
        Dim Range1 As Range
        Dim Range2 As Range
        Set Range1 = Range("A1")
        Set Range2 = Range("A2")
        Range1.Value = 123
        Range2.Value = 456
        
        CalledProcedure R1:=Range1, R2:=Range2
    End Sub
    
    Sub CalledProcedure(ByRef R1 As Range, ByVal R2 As Range)
        R1.Value = 321
        R2.Value = 654
        
        Set R1 = Range("A3")
        Set R2 = Range("A4")
    End Sub

I found the above code from your second link. What it does is define the variables in the calling sub, then redefines them in the called sub.

I'm looking for something that only requires defining variables in the called sub to reduce redundancy. Is that possible?

Something more along the lines of this code that definitely doesn't work:
Code:
    Sub CallingProcedure()
        CalledProcedure R1:=Range1, R2:=Range2
    End Sub
    
    Sub CalledProcedure(ByRef R1 As Range, ByVal R2 As Range)
        R1.Value = 321
        R2.Value = 654
        
        Set R1 = Range("A3")
        Set R2 = Range("A4")
    End Sub

You have to either pass the values via the calling procedure, or use public variables for values you want to apply to more than one procedure in a module. I don't know of any other way to do it.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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