VBA User Defined Function Ifelse(

xcelljunkie

New Member
Joined
Feb 27, 2013
Messages
10
Hi Gurus,

I'm somewhat new to vba and I'll bet it won't take more than a minute for one you experts to be able to solve this.
I'm trying to write a udf that will function similar to the iferror function. However, instead of looking for an error, I would like to test if the equation is equal to value x. For example: =iftest(vlookup(a1,B:C,2,0),x,y)
The idea is that i would be able to test if vlookup(a1,B:C,2,0)=x If this is true, then return the result of the vlookup. If false, then return y.

I'm trying to save time and increase readability of a nested ifs by eliminating the need of re-typing/copy and pasting redundant formulas in an already complex equation. For example.

Long way =if(vlookup(a1,B:C,2,0)=x,vlookup(a1,B:C,2,0),y)
Shortway =if(vlookup(a1,B:C,2,0),x,y)

I have made an attempt below, but this doesn't seem to work, and I am not sure where to go from here.

Function Iftest(varFormula, xtest, yvalue)
If varFormula = xtest Then
varFormula
Else
yvalue
End If
End Function

Thanks in advance!
 
I think this is it. This version addresses the Error values and "100"<"23" issues that post 9 has.
Code:
Function IfTest(ByVal formulaValue As Variant, ByVal testCriteria As Variant, Replacement As Variant) As Variant
    Dim qt As String:   qt = Chr(34)
    Dim strCompSymbol As String, strCValue As String, strFValue As String
    
    testCriteria = CStr(testCriteria)
    
    If (Left(testCriteria, 2) = "<>") Or (Left(testCriteria, 2) = "<=") Or (Left(testCriteria, 2) = ">=") Then
        strCompSymbol = Left(testCriteria, 2)
        strCValue = Mid(testCriteria, 3)
    ElseIf testCriteria Like "[<>=]*" Then
        strCompSymbol = Left(testCriteria, 1)
        strCValue = Mid(testCriteria, 2)
    Else
        strCompSymbol = "="
        strCValue = testCriteria
    End If
    
    If IsNumeric(strCValue) Then
        strCValue = CStr(Val(strCValue))
    Else
        strCValue = qt & strCValue & qt
    End If
    
    strFValue = CStr(formulaValue)
    If IsNumeric(strFValue) Then
        strFValue = CStr(Val(strFValue))
    Else
        strFValue = qt & strFValue & qt
    End If
    
    
    If Evaluate(strFValue & strCompSymbol & strCValue) Then
        IfTest = Replacement
    Else
        IfTest = formulaValue
    End If
End Function
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,216,082
Messages
6,128,700
Members
449,464
Latest member
againofsoul

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