VBA 的新手,我正在尝试创建一个函数

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

我正在尝试创建一个模仿我的公式的用户定义函数。我需要比我的公式更有效的东西。

我试过 VBA 并按原样使用上面的公式。这对我正在使用的较大数据集无效。

=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")))))))

模仿公式的VBA函数。

如果您不想向 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= "") 在逻辑上是不正确的。当与 AND 一起使用时,B2>10B2="" 永远不会 True,因此您可能需要弄清楚您的意图。

开始吧。总计是在调用函数的单元格中返回的值。 在示例中,它将是单元格 a2,B 将是单元格 b2 =caseTest(A2,B2)

根据 Scott 的说明,使用了 <10 而不是 null。欢迎编辑。

如果满足none个条件,也有一个默认值。编码愉快。

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

如果您需要有关创建自定义函数的帮助,下面 link 是一个有用的指南 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) 该单元格将根据上述逻辑显示值。详细解释在上面的link。

备注

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

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