New to VBA and I'm trying to create a function

时间:2019-01-09 22:26:46

标签: excel vba excel-formula ms-office

I'm attempting to make a user-defined function that mimics my formula. I need something more efficient than my formula.

I've tried VBA and using the above formula as is. This is ineffective for the larger datasets I'm working with.

=IF((AND(B2>=65,A2>=7)),"Greenbox",IF((AND(B2>10,A2=0,B2= "")),"Balance",IF((AND(B2>=65,A2<3)),"Yellowbox",IF((AND(B2<65,A2>=7)),"Purplebox",IF((AND(B2<65,A2<=3,A2>=1)),"Orangebox",IF(AND(B2>=65,A2<7,A2>=3),"Bluebox",IF(AND(A2<7,A2>=3,B2<65),"Redbox")))))))

A VBA function that mimics the formula.

3 个答案:

答案 0 :(得分:1)

如果您不想向UDF提供输入,则可以使用Application.Caller获取行号和工作表。否则,您可以为A2B2范围添加两个参数,并在那里比较值。

我没有对这两种方法进行性能测试,但我想使用Application.Caller的方法将是性能更高的方法-但我想出了另一个示例不会受伤。

Application.Caller方法

Function myFunc() As String

    Dim r As Long, ws As Worksheet
    Set ws = Application.Caller.Worksheet
    r = Application.Caller.Row

    If ws.Cells(r, "B").Value >= 65 And ws.Cells(r, "A").Value >= 7 Then
        myFunc = "Greenbox"
    ElseIf ws.Cells(r, "B").Value > 10 And ws.Cells(r, "A").Value = 0 Then
        myFunc = "Balance"
    '.... Continue
    End If

End Function

工作表公式如下所示:=myFunc()(无需任何参数)


带参数方法的功能

Function myFunc(rngA As Range, rngB As Range) As String

    If rngB.Value >= 65 And rngA.Value >= 7 Then
        myFunc = "Greenbox"
    ElseIf rngB.Value > 10 And rngA.Value = 0 Then
        myFunc = "Balance"
    '.... Continue
    End If

End Function

工作表公式如下:=myFunc($A2, $B2)

正如斯科特·克里纳(Scott Craner)的评论中已经提到的,AND(B2>10,A2=0,B2= "")在逻辑上是不正确的。与B2>10一起使用时,B2=""AND永远不会是 True ,因此您可能需要弄清楚该意图。

答案 1 :(得分:0)

这是一个去。总计是在具有调用函数的单元格中返回的值。 在示例中,它将是单元格a2,而B将是单元格b2 = caseTest(A2,B2)

根据Scott的注释,使用<10而不是null。随时进行编辑。

如果不满足任何条件,则还有一个默认值。编码愉快。

Function caseTest(A, B)

Dim scoreA As Integer, scoreB As Integer, result As String
    scoreA = A.Value
scoreB = B.Value

If ((scoreA >= 7) And (scoreB >= 65)) Then
    Total = "Greenbox"
ElseIf ((scoreA = 0) And (scoreB <10)) Then
    Total = "Balance"
ElseIf ((scoreA < 3) And (scoreB >= 65)) Then
    Total = "Yellowbox"
ElseIf ((scoreA >= 7) And (scoreB < 65)) Then
    Total = "Purplebox"
ElseIf ((scoreA <= 3) And (scoreA >= 1) And (scoreB < 65)) Then
    Total = "Orangebox"
ElseIf ((scoreA >= 3) And (scoreA < 7) And (scoreB >= 65))) Then
    Total = "Bluebox"
ElseIf ((scoreA >= 3) And (scoreA < 7) And (scoreB < 65))) Then
    Total = "Redbox"
Else
    Total = "default"
End If
    caseTest = Total
End Function

答案 2 :(得分:0)

如果您需要有关创建自定义函数的帮助,则下面的链接是有用的指南 https://support.office.com/en-us/article/create-custom-functions-in-excel-2f06c10b-3622-40d6-a1b2-b6748ae8231f

为回答您的问题,我根据从您的公式中得出的内容创建了一个自定义函数。

Function Cust_SetBox(A as Range, B as Range) As String
'function will receive two parameters A and B as ranges and return back a string
   Application.Volatile 'this ensures that formula will update when cell values are modified

   'Original formula
   '=IF((AND(B2>=65,A2>=7)),"Greenbox",IF((AND(B2>10,A2=0,B2= "")),"Balance",IF((AND(B2>=65,A2<3)),"Yellowbox",IF((AND(B2<65,A2>=7)),"Purplebox",IF((AND(B2<65,A2<=3,A2>=1)),"Orangebox",
   'IF(AND(B2>=65,A2<7,A2>=3),"Bluebox",IF(AND(A2<7,A2>=3,B2<65),"Redbox")))))))

   'adding .value="" condition as emtpy cells will show up as true while checking for X.Value<n
   If B.Value = "" And A.Value = "" Then
       Cust_SetBox = "Unknown"
   ElseIf (B.Value = "" Or B.Value > 10) And A.Value = 0 Then 'you might want to check this condition as it is not clear from your formula
       Cust_SetBox = "Balance"
   ElseIf B.Value >= 65 And B.Value <> "" Then
       If A.Value >= 7 Then
           Cust_SetBox = "Greenbox"
       ElseIf A.Value < 3 Then
           Cust_SetBox = "Yellowbox"
       ElseIf A.Value < 7 And A.Value >= 3 Then
           Cust_SetBox = "Bluebox"
       End If
   ElseIf B.Value < 65 And B.Value <> "" Then
       If A.Value >= 7 Then
           Cust_SetBox = "Purplebox"
       ElseIf A.Value <= 3 And A.Value >= 1 Then
           Cust_SetBox = "Orangebox"
       ElseIf A.Value < 7 And A.Value >= 3 Then
           Cust_SetBox = "Redbox"
       End If
   Else
       Cust_SetBox = "Unknown"
   End If



End Function
若要将此功能快速添加到您的工作簿。 使用Alt + F11,插入一个新模块并将上述代码添加到该模块中。您应该能够将此新函数用作公式。 转到任何单元格,然后输入以下内容 = Cust_SetBox(A1,B1) 单元格将根据上述逻辑显示值。在上面的链接中有详细说明。

注意

确保将“计算”设置为自动计算(在“公式”菜单->“计算”选项中),否则按F9键进行计算

该工作簿必须另存为启用宏的文件,否则该功能将在以后不可用。