无法使用自定义VB函数的Excel数组公式。

huangapple go评论73阅读模式
英文:

Failing to use an Excel array formula with a custom VB function

问题

我正在尝试使用Excel数组公式来计算列范围(如果你愿意,也可以称为数组)中具有背景颜色的单元格数量。

要检查单元格是否具有背景颜色,我使用了这个函数:

Function FillIndicator(color As Range) As Integer
If color.Interior.ColorIndex = -4142 Then
    FillIndicator = 0
Else
    FillIndicator = 1
End If
End Function

这个函数是有效的。

如果我要避免使用数组公式,假设我的范围是C5:C7,我需要写类似于以下内容:

=FillIndicator(C5) + FillIndicator(C6) + FillIndicator(C7)

现在,我尝试使用数组公式的幼稚尝试是:

=SUM(FillIndicator(C5:C7))

然后按下Ctrl+Shift+Enter。但是这只会得到一个#VALUE错误。

我做错了什么?还有 - 我可以使这个工作吗?

注意:我使用的是Excel 365版本2208。我想是这样的。

英文:

I'm trying to use Excel array formulae for summing up the number of cells in a columnar range (an array if you will), which have a background color.

For checking whether a cell has a background color, I use this function:

Function FillIndicator(color As Range) As Integer
If color.Interior.ColorIndex = -4142 Then
    FillIndicator = 0
Else
    FillIndicator = 1
End If
End Function

which works.

If I were to eschew array formulae, and my range were, say, C5:C7, I would need to write something like:

=FillIndicator(C5) + FillIndicator(C6) + FillIndicator(C7)

Now, my naive attempt at using an array formula here was writing:

=SUM(FillIndicator(C5:C7))

and pressing <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Enter</kbd>. But that just gets me a #VALUE error.

What am I doing wrong? And - can I make this work?

Note: I'm using Excel 365 version 2208. I think.

答案1

得分: 1

以下是您需要翻译的代码部分:

Function SumCellsByColor(colorRange As Range, targetColor As Long) As Long
    Dim cell As Range
    Dim count As Long
    
    count = 0
    
    For Each cell In colorRange
        If cell.Interior.color = targetColor Then
            count = count + 1
        End If
    Next cell
    
    SumCellsByColor = count
End Function


Sub ExampleUsage()
    Dim rng As Range
    Dim count As Long
    
    ' Define the range of cells in which you want to count
    Set rng = Range("A18:C22")
    
    ' Call the SumCellsByColor function and pass the range and target color.
    count = SumCellsByColor(rng, RGB(255, 0, 0)) ' For example, the color red
    
    ' Print the result
    MsgBox "Number of cells with red color: " & count
End Sub

请注意,这是用于在特定列中计算单元格或范围的颜色的代码。

英文:

I think this is what you need to count cells or ranges of specific columns.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

Function SumCellsByColor(colorRange As Range, targetColor As Long) As Long
    Dim cell As Range
    Dim count As Long
    
    count = 0
    
    For Each cell In colorRange
        If cell.Interior.color = targetColor Then
            count = count + 1
        End If
    Next cell
    
    SumCellsByColor = count
End Function


Sub ExampleUsage()
    Dim rng As Range
    Dim count As Long
    
    &#39; Define the range of cells in which you want to count
    Set rng = Range(&quot;A18:C22&quot;)
    
    &#39; Call the SumCellsByColor function and pass the range and target color.
    count = SumCellsByColor(rng, RGB(255, 0, 0)) &#39; For example, the color red
    
    &#39; Print the result
    MsgBox &quot;Number of cells with red color: &quot; &amp; count
End Sub

<!-- end snippet -->

答案2

得分: 1

以下是您要翻译的代码部分:

这个答案与@RicardinhoL的答案相同只有三个小的不同之处
1使用默认值的可选颜色参数您在问题2中使用
2检查范围是否为空否则会出现错误
3使用函数的名称作为计数器因为它本身像一个变量一样工作

必须返回Long因为Excel中有很多单元格

Public Function CountIfColor(rngClr As Range, Optional thisColor As Long = -4142) As Long
   Dim r As Range
   If Not rngClr Is Nothing Then
      For Each r In rngClr
         If r.Interior.ColorIndex = thisColor Then CountIfColor = CountIfColor + 1
      Next
   End If
End Function
英文:

This answer is the same as @RicardinhoL answer with three small differences: 1) use an Optional color parameter with default value that you use in question 2) check if range is nothing else an error occurs 3) use as counter the name of the function, since it works like a variable itself.

Must to return Long because there are many-many cells in excel!

Public Function CountIfColor(rngClr As Range, Optional thisColor As Long = -4142) As Long
   Dim r As Range
   If Not rngClr Is Nothing Then
      For Each r In rngClr
         If r.Interior.ColorIndex = thisColor Then CountIfColor = CountIfColor + 1
      Next
   End If
End Function

答案3

得分: 1

要使UDF返回一个数组,你只需在返回行将数组设置为该函数。

例如,假设你想返回一个布尔数组,表示范围中的单元格是否着色:

Option Explicit

Function FillIndicator(color As Range)
    Dim v  '循环遍历数组的变量
    Dim al As Object '收集测试结果
    
Set al = CreateObject("System.Collections.ArrayList")

'在每个单元格上运行测试
For Each v In color
    al.Add v.Interior.ColorIndex <> -4142
Next v

'将函数设置为一个数组
FillIndicator = al.toarray

End Function

请注意倒数第二行,FillIndicator 被设置为该数组。

这个公式将根据 Interior.ColorIndex 属性返回一个 TRUE/FALSE 数组。

然后你可以像处理其他数组函数一样处理它。

英文:

To have the UDF return an array, you merely need to set the array equal to the function in the return line.

For example, given your apparent desire to return a boolean array corresponding to whether or not the cells in a range are colored:

Option Explicit

Function FillIndicator(color As Range)
    Dim v  &#39;variable to loop through array
    Dim al As Object &#39;collect the test results
    
Set al = CreateObject(&quot;System.Collections.ArrayList&quot;)

&#39;run the test on each cell
For Each v In color
    al.Add v.Interior.ColorIndex &lt;&gt; -4142
Next v

&#39;Set the Function equal to an array
FillIndicator = al.toarray

End Function

Note the penultimate line where FillIndicator is set equal to the array.

The formula will return an array of TRUE/FALSE depending on the Interior.ColorIndex property.

You can then treat this the same as any other array function.

huangapple
  • 本文由 发表于 2023年5月14日 19:09:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定