counting items in list based on criteria

rjwebgraphix

Well-known Member
Joined
May 25, 2010
Messages
590
I've done the following code and it works, but it's kind of slow. Does anyone have a better/faster way to count items in a list based on criteria?

The function that counts:
Code:
Function partcount(Strm As String, partnum As String)
Dim row As Long
Dim itemcnt As Long
row = 2
itemcnt = 0
Do While Sheets("Datasheet").Cells(row, "A") <> ""
    If Sheets("Datasheet").Cells(row, "C") = Strm And Sheets("Datasheet").Cells(row, "D") = partnum Then
        itemcnt = itemcnt + 1
    End If
row = row + 1
Loop
partcount = itemcnt
End Function

The code where the function is used:
Code:
Sub newusages()
Dim TotalPartCount As Long
Dim row As Long
 
Sheets("Usage").Activate
 
Rows("1:65000").Select
Selection.Delete Shift:=xlUp
 
Sheets("Datasheet").Cells(1, "U") = Sheets("Datasheet").Cells(1, "G")
Sheets("Datasheet").Cells(2, "U") = "USED"
Sheets("Datasheet").Cells(1, "V") = Sheets("Datasheet").Cells(1, "H")
Sheets("Datasheet").Cells(2, "V") = "ERSA"
 
Sheets("Datasheet").Range("C1:I65000").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Datasheet").Range("U1:V2"), _
CopyToRange:=Sheets("Usage").Range("A1:G1"), Unique:=True
 
Sheets("Datasheet").Cells(1, "U").ClearContents
Sheets("Datasheet").Cells(2, "U").ClearContents
Sheets("Datasheet").Cells(1, "V").ClearContents
Sheets("Datasheet").Cells(2, "V").ClearContents
'** Recorded move fields
    Columns("C:C").Select
    Selection.Cut
    Columns("H:H").Select
    Selection.Insert Shift:=xlToRight
    Columns("C:C").Select
    Selection.Cut
    Columns("G:G").Select
    Selection.Insert Shift:=xlToRight
    Range("F12").Select
'** end recording
 
Sheets("Usage").Cells(1, "H") = "Total"

row = 2
Do While Sheets("Usage").Cells(row, "A") <> ""
    
    TotalPartCount = partcount(Sheets("Usage").Cells(row, "A"), Sheets("Usage").Cells(row, "B"))
    Cells(row, "G") = TotalPartCount
    Cells(row, "H") = "=F" & row & "*G" & row
row = row + 1
Loop
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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