简体   繁体   English

如何在VBA中的矩阵中跨行检查条件

[英]How can I check for a condition across rows in a matrix in VBA

Input: I have a range in an excel sheet. 输入:我在Excel工作表中有一个范围。 Let's say B1:F100 = 100X5 matrix on "Sheet 1". 假设“ Sheet 1”上的B1:F100 = 100X5矩阵。

Now I want to count the number of rows that have "Data" in any of the columns B to F (from rows 1 to 100). 现在,我要计算从B到F的任何列(从1到100行)中都包含“数据”的行数。 ie I'm looking for a function, let's say, ExistIfRow(B1:F100) that will return a 100X1 array of 0s or 1s. 即我正在寻找一个函数,比如说ExistIfRow(B1:F100),它将返回一个0X或1S的100X1数组。 So I can simply do a sum(ExistIfRow(B1:F100)) to get the number of rows. 所以我可以简单地做一个sum(ExistIfRow(B1:F100))以获取行数。 I would like to be able to select 100 cells and enter this function as an array formula to get that 100X1 result in the sheet. 我希望能够选择100个单元格并将此函数作为数组公式输入,以在工作表中获得该100X1的结果。

I hope that makes sense. 我希望这是有道理的。


In addition, I attempted to create this function but it doesn't show up in my excel sheet when I try to put it in a cell. 另外,我尝试创建此函数,但是当我尝试将其放在单元格中时,该函数未显示在我的excel工作表中。 Can someone please help me see what I am doing wrong? 有人可以帮我看看我在做什么错吗? This function exists under "Modules" in the worksheet. 此功能存在于工作表中的“模块”下。

Function RowWiseOR(Rin As Range, Condition As Variant) As Range

Dim rw As Range
Dim Out As Variant
Dim i As Integer

i = 0

For Each rw In Rin.Rows
    If WorksheetFunction.CountIf(rw, Condition) > 0 Then Out(i).Value = 1 Else Out(i).Value = 0
    i = i + 1
End

RowWiseOR = Out
End Function

I'm not sure why "it doesn't show up", but there were a few issues with the function itself. 我不确定为什么“它没有显示”,但是函数本身存在一些问题。 Specifically, 特别,

  • The function should return a Variant array, not a Range 该函数应返回Variant数组,而不是Range
  • The variable Out needed to be initialized as an array to hold the results 需要将变量Out初始化为数组以保存结果
  • Elements of arrays don't have a .Value property. 数组的元素没有.Value属性。 For example, use Out(i) = 1 instead of Out(i).Value = 1 例如,使用Out(i) = 1代替Out(i).Value = 1
  • Transpose the array before writing it out to the sheet if you want it to be a column vector. 如果希望将其作为列向量,请在将其写出到图纸之前Transpose置数组。

See below for the revised function: 修改后的功能,请参见以下内容:

Function RowWiseOR(Rin As Range, Condition As Variant) As Variant

    Dim rw As Range
    Dim Out As Variant
    Dim i As Integer

    i = 0

    ReDim Out(Rin.Rows.Count - 1)

    For Each rw In Rin.Rows
        If WorksheetFunction.CountIf(rw, Condition) > 0 Then Out(i) = 1 Else Out(i) = 0
        i = i + 1
    Next

    RowWiseOR = Application.Transpose(Out)

End Function

Remember to enter it as an array formula. 请记住将其作为数组公式输入。 See here for more information. 有关更多信息,请参见此处

The reason why Countif is giving an error because countif does not work with array. Countif之所以给出错误的原因是因为countif不适用于数组。 Try the same in excel and you will again get an error. 在excel中尝试相同的操作,您将再次收到错误消息。

Also i do not understand why you need to create a function to count 0's or 1's. 我也不明白为什么你需要创建一个函数来计数0或1。 this can be easly achieved by countif formula in excel - COUNTIF(A1:C3,"data") 这可以通过excel中的countif公式轻松实现-COUNTIF(A1:C3,“ data”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM