Input: I have a range in an excel sheet. Let's say B1:F100 = 100X5 matrix on "Sheet 1".
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). ie I'm looking for a function, let's say, ExistIfRow(B1:F100) that will return a 100X1 array of 0s or 1s. So I can simply do a sum(ExistIfRow(B1:F100)) to get the number of rows. 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.
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. 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,
Variant
array, not a Range
Out
needed to be initialized as an array to hold the results .Value
property. For example, use Out(i) = 1
instead of Out(i).Value = 1
Transpose
the array before writing it out to the sheet if you want it to be a column vector. 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. Try the same in excel and you will again get an error.
Also i do not understand why you need to create a function to count 0's or 1's. this can be easly achieved by countif formula in excel - COUNTIF(A1:C3,"data")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.