WorksheetFunction.CountIf with a range of values as criteria

MSalvador

New Member
Joined
Jan 21, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi!

I've been dealing with a problem which I can not find information about it. So what I want to make is a count of records depending on a criteria, I know I can accomplish it with WorksheetFunction.CountIf but let me explain a bit further. The problem I face is giving the criteria argument as a range like as a matrix formula (I believe that is the thing) as in the next image:

1705896689077.png


But for WorksheetFunction.CountIf it appear does not work the same way as in a formula in a cell giving me a Type Mismatch error. I make it work with WorksheetFunction.XLookup, being my code:

VBA Code:
Dim wb As Workbook
Dim ws As Worksheets, wsTable As Worksheets
Dim tableInfo As ListObject
Dim columnCompanyNameInfo As ListColumn, columnCompanyStateInfo As ListColumn
Dim lastRowWS As Integer

lastRowWS = ws.UsedRange.Rows.Count
    
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet1")
Set wsTable = wb.Worksheets("Table")
Set tableInfo = wsTable.ListObjects("Table1")
Set columnCompanyNameInfo = tableInfo.ListColumns("Company_Name")
Set columnCompanyStateInfo = tableInfo.ListColumns("Company_State")

ws.Range("G2", "G" & lastRowWS).Value = WorksheetFunction.XLookup(ws.Range("B2", ws.Range("B2").End(xlDown)).Value, columnCompanyNameInfo.DataBodyRange, columnCompanyStateInfo.DataBodyRange, 1)

For visual reference:

1705896642378.png


Is there a way I can replicate what I did with the XLookup in the CountIf function? Or the For loop it's my only solution? Below is the code I am trying but failed to get it work.

VBA Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim lastRowWS As Integer

Dim table As ListObject
Dim columnConcat As ListColumn

Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")

Set table = ws.ListObjects("Table1")
Set columnConcat = table.ListColumns("ConcatID_CompanyName")

lastRowWS = ws.UsedRange.Rows.Count

ws.Range("A2", "A" & lastRowWS).Value = WorksheetFunction.CountIf(columnConcat.DataBodyRange, ws.Range("A2", "A" & lastRowWS).Value &"-"&ws.Range("B2", "B" & lastRowWS).Value)

For visual reference:
1705897006563.png


Thanks for your responses!
 

Attachments

  • 1705896118271.png
    1705896118271.png
    73.6 KB · Views: 3

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the MrExcel board!

Why are you trying to do the calculations with vba rather than just putting formulas in the sheet like you have shown?

If you really want/need to use vba to get the values, then could you use an idea like this? I have not used ListObjects, different worksheets etc like you seem to have as i don't have the details, but starting with the data in column C, the code below has produced the values in columns F:G

MSalvador.xlsm
CDEFG
1
2
3
4Column3Column34
5Column5Column52
6Column3Column27
7Column2Column45
8Column4Column13
9Column1
10Column3
11Column5
12Column4
13Column4
14Column2
15Column2
16Column1
17Column2
18Column4
19Column1
20Column2
21Column3
22Column2
23Column4
24Column2
25
Sheet1


VBA Code:
Sub Test_Countif()
  With Range("F4")
    .Formula2 = "=LET(r,C4:C" & Range("C" & Rows.Count).End(xlUp).Row & ",u,UNIQUE(r),HSTACK(u,COUNTIF(r,u)))"
    .SpillingToRange.Value = .SpillingToRange.Value
  End With
End Sub
 
Upvote 0
Welcome to the MrExcel board!

Why are you trying to do the calculations with vba rather than just putting formulas in the sheet like you have shown?

If you really want/need to use vba to get the values, then could you use an idea like this? I have not used ListObjects, different worksheets etc like you seem to have as i don't have the details, but starting with the data in column C, the code below has produced the values in columns F:G

MSalvador.xlsm
CDEFG
1
2
3
4Column3Column34
5Column5Column52
6Column3Column27
7Column2Column45
8Column4Column13
9Column1
10Column3
11Column5
12Column4
13Column4
14Column2
15Column2
16Column1
17Column2
18Column4
19Column1
20Column2
21Column3
22Column2
23Column4
24Column2
25
Sheet1


VBA Code:
Sub Test_Countif()
  With Range("F4")
    .Formula2 = "=LET(r,C4:C" & Range("C" & Rows.Count).End(xlUp).Row & ",u,UNIQUE(r),HSTACK(u,COUNTIF(r,u)))"
    .SpillingToRange.Value = .SpillingToRange.Value
  End With
End Sub
Hi Peter! Thanks for the welcoming!

This step its part of a data cleaning process I am working on, that is why I am doing it in vba, also I thought making it work with the built-in countif for vba would be more efficient than trying to create a workaround solution. As for the different objects I use, the main purpose for writting them down was merly for if anyone saw something strange about the use of these elements that could be the reason of the error I get.

I have not found anything related to why I can not replicate what I did with the XLookup formula, but this solution does the trick, thanks!

I will still be looking the cause for the error I get on the CounIf vba formula hoping I can get an answer either is possible or not, just for the knowledge.
 
Upvote 0
I will still be looking the cause for the error I get on the CounIf vba formula hoping I can get an answer either is possible or not, just for the knowledge.
Your code and your image in Post #1 for the Countifs don't line up at all. This is based on your image.

VBA Code:
Sub fncCountIf()

    Dim rng As Range, rCell As Range
    Dim arr1, arr2
    Dim lastRowWS As Long
    Dim i As Long
   
    lastRowWS = 8
   
    Set rng = Range("D4:D24")
   
    arr1 = Range("G4:G" & lastRowWS).Value
    arr2 = Range("H4:H" & lastRowWS).Value
   
    For i = 1 To UBound(arr1)
        arr1(i, 1) = arr1(i, 1) & arr2(i, 1)
    Next i
   
    Range("I4:I" & lastRowWS).Value = Application.CountIfs(rng, arr1)

End Sub

20240127 VBA Countifs MSalvador.xlsm
ABCDEFGHI
1
2
3IDNameConcatPriceIDNamePrice
4ID 3Column3ID 3Column310ID 1Column11
5ID 5Column5ID 5Column550ID 2Column22
6ID 3Column3ID 3Column350ID 3Column33
7ID 2Column2ID 2Column230ID 4Column43
8ID 4Column4ID 4Column410ID 5Column52
9ID 1Column1ID 1Column150
10ID 3Column3ID 3Column320
11ID 5Column5ID 5Column530
12ID 4Column4ID 4Column440
13ID 4Column4ID 4Column450
14ID 2Column2ID 2Column210
Sheet1 (2)
Cell Formulas
RangeFormula
D4:D14D4=B4&C4
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top