ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Passing Values Syntax (https://www.excelbanter.com/excel-programming/287529-passing-values-syntax.html)

Chuck Taylor

Passing Values Syntax
 
I'm trying to create a tool that values various stock option
positions. I want to read in option values (the strike and cost) and
pass that information into prodcedures that calculate and graph the
payoff.

I'm having trouble passing the information from procedure to
procedure. A bit of pseudo-code might help:

Sub BullSpread()
GetPrices(strike1,strike2,cost1,cost2)
if strike1<shareprice then
blah blah
endif
.....
end sub

sub getprices()
strike1=range("A1")
strike2=range("B1")
......
end sub

I can't seem to get the GetPrices routine to pass the bits of info -
debugging shows the variables as "empty". I can't seem to figure out
what goes in the parentheses when calling/returning.

Thanks for any help.

TerryK[_2_]

Passing Values Syntax
 
You can Dim your varialbes in the Declarations arrear of the module as shown. This should then work for you. Step through the code and you will see x change to the appropriate value after each line is read.

Dim strike1, strike2, cost1, cost2

Sub BullSpread()
GetPrices
x = strike1
x = strike2
x = cost1
x = cost2

End Sub

Sub GetPrices()
strike1 = 1 ' Range("A1")
strike2 = 2 ' Range("B1")
cost1 = 3
cost2 = 4
End Function

TerryK



Bob Phillips[_6_]

Passing Values Syntax
 
Chuck,

Firstly, add Option Explicit to the top of your code module. Then make sure
you declare your variables. Your code should look something like this

Sub BullSpread()
Dim SharePrice as Double
SharePrice = GetPrices(Range("A1"),Range("B1"),Range("C1"),Rang e("D1")
if Range("A1") < Shareprice then
blah blah
endif
......
end sub

sub getprices(strike1 as double, strike2 as double, cost1 as double, codt2
as double)
.......
end sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Chuck Taylor" wrote in message
...
I'm trying to create a tool that values various stock option
positions. I want to read in option values (the strike and cost) and
pass that information into prodcedures that calculate and graph the
payoff.

I'm having trouble passing the information from procedure to
procedure. A bit of pseudo-code might help:

Sub BullSpread()
GetPrices(strike1,strike2,cost1,cost2)
if strike1<shareprice then
blah blah
endif
.....
end sub

sub getprices()
strike1=range("A1")
strike2=range("B1")
......
end sub

I can't seem to get the GetPrices routine to pass the bits of info -
debugging shows the variables as "empty". I can't seem to figure out
what goes in the parentheses when calling/returning.

Thanks for any help.




onedaywhen

Passing Values Syntax
 
Bob,
I agree the arguments of GetPrices should be ByRef but I think
BullSpread should be passing (empty) variables for GetPrices to fill.
And you do realise that your getprices sub isn't returning a value to
the Shareprice variable, right?

I think this is what's required:

Sub BullSpread()

Dim strike1 As Double, strike2 As Double, cost1 As Double, cost2 As
Double

GetPrices strike1, strike2, cost1, cost2

If strike1<shareprice Then
' blah blah
End If

End Sub

Sub GetPrices (ByRef strike1 As Double, ByRef strike2 As Double, _
ByRef cost1 As Double, cost2 As Double)

strike1 = Range("A1").Value
strike2 = Range("B1").Value
cost1 = Range("C1").Value
codt2 = Range("D1").Value

End Sub

--

"Bob Phillips" wrote in message ...
Chuck,

Firstly, add Option Explicit to the top of your code module. Then make sure
you declare your variables. Your code should look something like this

Sub BullSpread()
Dim SharePrice as Double
SharePrice = GetPrices(Range("A1"),Range("B1"),Range("C1"),Rang e("D1")
if Range("A1") < Shareprice then
blah blah
endif
.....
end sub

sub getprices(strike1 as double, strike2 as double, cost1 as double, codt2
as double)
......
end sub


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Chuck Taylor" wrote in message
...
I'm trying to create a tool that values various stock option
positions. I want to read in option values (the strike and cost) and
pass that information into prodcedures that calculate and graph the
payoff.

I'm having trouble passing the information from procedure to
procedure. A bit of pseudo-code might help:

Sub BullSpread()
GetPrices(strike1,strike2,cost1,cost2)
if strike1<shareprice then
blah blah
endif
.....
end sub

sub getprices()
strike1=range("A1")
strike2=range("B1")
......
end sub

I can't seem to get the GetPrices routine to pass the bits of info -
debugging shows the variables as "empty". I can't seem to figure out
what goes in the parentheses when calling/returning.

Thanks for any help.



All times are GMT +1. The time now is 01:33 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com