JohnGow383
Board Regular
- Joined
- Jul 6, 2021
- Messages
- 141
- Office Version
- 2013
- Platform
- Windows
Hi,
Having trouble counting items in a table using VBA. I'll post a very simple example here to explain.
I am trying to use VBA to count for each row all the cells with "x" in the above table, only when the cell in Col C = "A"
Here is my code, but it's wrong of course. It is only counting the first row. The answer should be 2+3+3+2=10
Any help appreciated thanks
Having trouble counting items in a table using VBA. I'll post a very simple example here to explain.
I am trying to use VBA to count for each row all the cells with "x" in the above table, only when the cell in Col C = "A"
Here is my code, but it's wrong of course. It is only counting the first row. The answer should be 2+3+3+2=10
VBA Code:
Dim ws1 As Worksheet
Dim cell, rng As Range
Dim i, Count As Long
Sub test()
Set ws1 = Sheet1
Set rng = ws1.Range("C4:C13")
For i = 4 To 13
For Each cell In rng
If ws1.Cells(i, 3).Value = "A" Then
Count = Application.WorksheetFunction.CountIf(Range(Cells(i, 4), Cells(i, 10)), "x")
End If
Next cell
Next i
MsgBox Count 'To test
End Sub
Any help appreciated thanks