Changing the Function Based on Inputs

Whylucky

New Member
Joined
Sep 24, 2013
Messages
39
I am stuck on a couple of problems but they generally boil down to 1 thing. Is it possible to have VBA change the function based on the inputs it receives? So if i have a " - " somewhere in my inputs it knows that it needs to run a different calculation? or even simply display the " - " in the answer box? Any help would be much appreciated. Thank you in advance.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Acctualy you can achieve it using simple formula:

Code:
=IF(IFERROR(SEARCH(" - ",B2),0)," - ",RANDBETWEEN(0,9))

Where you have your values in cell B2. If the value in B2 contains " - " you will get " - " and if it does not then you will get random number between 0 and 9. Of course you can replace randbetween function with any other function. If you can do it using formulas than for sure you can do it using VBA
 
Upvote 0
Code:
Function Bearing(od As Double, odt As Double, id As Double, idt As Double, yield As Double) As Double


    If od = "-" Then
        Bearing = "-"
        
    Else
        O = od - odt
        i = id + idt
        area = pi / 4 * (O ^ 2 - i ^ 2)
        Bearing = yield * area
    End If
    
End Function
 
Upvote 0
What do you actually have in the cells that you are passing to the function? Not how they appear in the cell but the value that appears in the Formula Bar.
 
Upvote 0
They are physical numbers, unless it isnt a bearing in which case it has a - mark. so the function calculates it unless it has a - then it gives me error, when i want it to just reply -
 
Upvote 0
Try:

Rich (BB code):
Function Bearing(od As Variant, odt As Double, id As Double, idt As Double, yield As Double) As Variant
    If od = "-" Then
        Bearing = "-"
    Else
        O = od - odt
        i = id + idt
        area = Pi / 4 * (O ^ 2 - i ^ 2)
        Bearing = yield * area
    End If
End Function
 
Upvote 0
fibanocci1101 your idea worked excluding spaces around the "-", i would still like to get it working in the VBA code though so it looks cleaner
 
Upvote 0

Forum statistics

Threads
1,223,761
Messages
6,174,344
Members
452,556
Latest member
Chrisolowolafe

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