Newbe help - VBA function

K

Kristen

Newbe needs help

I'm creating a function for Excel.
I want the function to operate two different ways depending on the
contents of a sell.

Like
If cell A1 (AB) is A then 1+2
If cell A1 (AB) is B then 1+3
so
Function test(AB, 1, 2)
???

Thankyou for your help
Kristen
 
T

Tushar Mehta

Ummm...have you looked into user-defined functions? Also, what do you
want the function to do? What does A1 (AB) mean? What do 1+2 and 1+3
mean? Do the numbers stand for themselves? If so, it would mean you
want the result to be 3 or 4. Or do the numbers represent something
else?

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Business solutions leveraging technology
Microsoft Most Valuable Professional (MVP) 2000-2004
 
K

Kristen

Like this
If cell A1 (AB in VBA function ) is A then Va * Vb
If cell A1 (AB) is B then Va * Vc


In Excel IF function terms I'm wanting to:

=IF(AB = "A",Va*Vb,IF(AB = "B",Va*Vc,"-"))

So what should my function be in VBA:
Function test(AB, Va, Vb, Vc)
?? where to from here

Thankyou for your help
Kristen
 
T

Tushar Mehta

One possible approach:

In VBA, create the following in a standard module:

Option Explicit

Function KristenUDF(AB As String, _
Va As Double, Vb As Double, Vc As Double)
Select Case UCase(AB)
Case "A": KristenUDF = Va * Vb
Case "B": KristenUDF = Va * Vc
Case Else: KristenUDF = "Unexpected value of AB (=" & AB & ")"
End Select
End Function

Then, use the UDF as
=KristenUDF(C2,D2,E2,F2)

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Business solutions leveraging technology
Microsoft Most Valuable Professional (MVP) 2000-2004
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top