Help with compile error?

  • Thread starter Thread starter newbie
  • Start date Start date
N

newbie

I wonder if some one can tell me what is wrong with my code?
I am having problems with the first line? I looked online and i
thought my syntax was correct?

Andrew

Function Process1(Reactant As Real, Yield As Real) As Real

Dim Reactant As Real
Dim Yield As Real

Set Cresult2 = 0

'Do

Set Reactant = Workbooks("Sayaka").Sheets("Sheet1").Range("D34")
Set Yield = Workbooks("Sayaka").Sheets("Sheet1").Range("D34")

Set Aresult = ((Reactant / 100) * (1 - Yield)) * 100
Set Bresult = ((Reactant / 100) * (1 - Yield)) * 200
Set Cresult = ((Reactant / 100) * (Yield)) * 300
........

End Function
 
First, unless you've defined your own type "Real", you need to use a
built-in type. Numeric values stored in XL cells are always of type
Double, so that's safe to use.

Second, you can't Dim a variable that you've already declared as an
argument in the function's argument list

Third, 'Set' is only used to assign an object variable to an object, for
instance, a range variable to a range as in

Dim rng As Range
Set rng = Workbooks("Sayaka").Sheets("Sheet1").Range("D34")

Now the variable rng can be used in place of the range object (in this
case, cell D34 of Sheet1 of workbook Sayaka).

Other (non-object) variables just use the assignment operator (=), so
for instance

Dim Reactant As Double
Dim Yield As Double
Reactant = rng.Value
Yield = rng.Value

So you might try:

Public Function Process1( _
ByVal Reactant As Double, _
ByVal Yield As Double) As Double
Dim AResult As Double
Dim BResult As Double
Dim CResult As Double

AResult = (Reactant / (1 - Yield))
BResult = (Reactant / (1 - Yield)) * 2
CResult = (Reactant / (1 - Yield)) * 3

Process1 = AResult + BResult + CResult
End Function

And call the function from the worksheet as

=Process1(Sheet1!D34, Sheet1!E34)
 
Back
Top