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:
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:
For visual reference:
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.
For visual reference:
Thanks for your responses!
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:
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:
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:
Thanks for your responses!