Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA新手,我正在尝试创建一个函数_Excel_Vba_Excel Formula_Ms Office - Fatal编程技术网

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

Excel VBA新手,我正在尝试创建一个函数,excel,vba,excel-formula,ms-office,Excel,Vba,Excel Formula,Ms Office,我正在尝试创建一个用户定义的函数,它模仿我的公式。我需要比我的配方更有效的东西 我尝试过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(B

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

我尝试过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获取行号和工作表。否则,您可以为A2和B2范围添加两个参数,并比较其中的值

我没有对这两种方法进行性能测试,但我可以想象不使用应用程序的方法。调用者将是性能更高的方法-但我认为另一个例子不会有什么影响

调用方方法 工作表公式的格式如下:=myFunc。不需要争论

带参数的函数方法 工作表公式如下所示:=myFunc$A2,$B2


正如Scott Craner在评论中已经提到的,B2>10,A2=0,B2=在逻辑上是不正确的。B2>10和B2=与and一起使用时将永远不会为真,因此您可能需要使用该选项来确定您的意图。

开始吧。Total是调用函数的单元格中返回的值。 在本例中,它将是单元a2,而B将是单元b2 =案例2,B2


根据Scott的说明,如果您需要有关创建自定义函数的帮助,下面的链接是一个有用的指南

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

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_SetBoxA1,B1 单元格将显示基于上述逻辑的值。详细解释请参见上面的链接。 注

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


工作簿必须另存为启用宏的文件,否则该函数稍后将不可用。

ANDB2>10,A2=0,B2=如何为真。B2不能同时大于10。你好,Rob,欢迎来到StackOverflow。一个技巧是激活菜单中的“开发人员”选项,录制新宏,执行所需操作并停止录制宏。然后,系统为您创建了VBA代码,您可以从一些有用的东西开始。要小心,因为如果你在同一个标签,位置上,它会起作用…我猜UDF很难比核心配方更有效。谢谢你-这太完美了
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
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
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