Countif用户定义函数返回#Value

时间:2019-02-11 20:44:28

标签: user-defined-functions countif

我正在尝试创建一个在不同列中具有互斥条件的vba countifs函数。如果C列中的相应行具有特定的文本值,则只需要计算E列中具有内部颜色的单元格即可。

从本质上讲,这是一个休假日历,但我需要它计算出谁去了(由颜色表示),但前提是他们具有特定的资格(由C列中的文本表示) 例如:如果C10的值为“ TL”并且E10的内部颜色为绿色,则仅对单元格E10进行计数

我尝试了许多不同的代码,但是它们都返回#VALUE!以下是我尝试过的示例:

 Function countifs_by_QASO(Cl As Range, Ct As Variant, Dy As Range, clr As 
 Variant) As Variant
 Application.Volatile True

 Dim x As Long
 Dim Cert As Range
 Dim Day As Range
 Dim Level As Variant
 Dim criteria_color As Variant

 Set Cert = Range("C21:C101")
 Set Day = Range("E21:E101")
 criteria_color = criteria.Interior.color

 x = 0

 For Each Cert In Cl
     If Level = Ct Then
     x = x + 1
End If

countifs_by_QASO = x

End Function

接下来我尝试了

 Function countifs_by_QASO(Cert As Range, Level As Variant, Day As Range, 
 color As Range) As Variant
     Application.Volatile True

 Dim y As Long
 Dim cel As Range
 Dim cel2 As Range

 criteria_color = criteria.Interior.color

 y = 0

 For Each cel In Day
     If cel.Interior.color = color.Interior.color Then
     y = y + 1
     End If
     Next

 For Each cel2 In Cert
     If cel = Level Then
     y = y + 0
     Else
     y = y - 1
     End If

 Next

 countifs_by_QASO = y

最后,我尝试在此处合并Offset属性:

Function countifs_by_QASO(Day As Range, color As Range) As String
Application.Volatile True

 Dim y As String
 Dim cel As Range
 Dim Level As Range
 color = criteria.Interior.color
 Level = Range("C21:C101")
 y = 0

 For Each cel In Day
     If cel.Interior.color = color.Interior.color Then
             If cell.Offset(0, Level).Value = "QASO" Then
                 y = y + 1
             End If
     End If

 Next

 countifs_by_QASO = y

 End Function

它们都返回#Value!我确定这反映了我的VBA经验不足

使用时,公式需要执行以下操作:

步骤1-向下查看其列

步骤2-如果单元格具有指定的内部颜色,则

第3步,将同一行中的行偏移到C列和

步骤4-如果该单元格具有指定的文本,则

第5步=计数+1

0 个答案:

没有答案