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!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,249
Members
449,075
Latest member
staticfluids

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