VBA Dimed Value Called in a Sub

Table83

New Member
Joined
Feb 25, 2010
Messages
16
This is a little hard to explain so bear with me.
I'm trying to dim a value, call a sub and use that dimmed value within the sub.

Code:
Sub InitialSub()
Dim Value1, Value2, Value3 As Double
Value1 = Worksheets("Sheet1").Range("A1")
Value1 = Worksheets("Sheet1").Range("A2")
Sub2
Worksheets("Sheet1").Range("A3") = Value3
End Sub
 
Sub Sub2()
Value3 = Value1 + Value2
End Sub

I'm still pretty new at VBA so any help would be appreciated. I'm creating a program for my investment society. I wrote the code all out in a new way. When I tried it and it totally flopped because of this reason. So close and yet so far!
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

sous2817

Well-known Member
Joined
Feb 22, 2008
Messages
2,276
Hello and welcome to the board!

A couple of things,

1) you can dim the variables at the top of the module (above any 'Sub') to make the public. This allows you to use them within other subs in the same module.

2) Your variables are miss dimmed a bit. The first two are dimmed as variants and only the last is dimmed as a double (unless that was your intent).

Nonetheless, this seemed to work for me in my test sheet:

Code:
Option Explicit
Dim Value1 As Double, Value2 As Double, Value3 As Double
Sub InitialSub()

Value1 = Worksheets("Sheet1").Range("A1")
Value2 = Worksheets("Sheet1").Range("A2")
Call Sub2
Worksheets("Sheet1").Range("A3") = Value3
End Sub
 
Sub Sub2()
Value3 = Value1 + Value2
End Sub
 

djreiswig

Well-known Member
Joined
Mar 13, 2010
Messages
523
Could use a function instead of a sub, and then just pass the values to the function.

Code:
 Sub InitialSub()
    Dim Value1 As Double, Value2 As Double, Value3 As Double
    Value1 = Worksheets("Sheet1").Range("A1")
    Value2 = Worksheets("Sheet1").Range("A2")
    Worksheets("Sheet1").Range("A3") = Sub2(Value1, Value2)
End Sub
 
Function Sub2(Value1 As Double, Value2 As Double) As Double
    Sub2 = Value1 + Value2
End Function
Or do away with the variables all together

Code:
Sub InitialSub()
    With Worksheets("Sheet1")
        .Range("A3") = Sub2(.Range("A1"), .Range("A2"))
    End With
End Sub
 
Function Sub2(Value1 As Double, Value2 As Double) As Double
    Sub2 = Value1 + Value2
End Function
 

Watch MrExcel Video

Forum statistics

Threads
1,127,445
Messages
5,624,817
Members
416,056
Latest member
VARSHA V VASWANI

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
Top