Help with VBA for Looping through and using Countif

JohnGow383

Board Regular
Joined
Jul 6, 2021
Messages
141
Office Version
  1. 2013
Platform
  1. Windows
Hi,

Having trouble counting items in a table using VBA. I'll post a very simple example here to explain.

Capture.JPG


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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
try this change:
VBA Code:
Sub test()


Set ws1 = Sheet1
Set rng = ws1.Range("C4:C13")
Count = 0
For i = 4 To 13
 
   For Each cell In rng
   
      If ws1.Cells(i, 3).Value = "A" Then
     
       Count = Count + Application.WorksheetFunction.CountIf(Range(Cells(i, 4), Cells(i, 10)), "x")
    
      End If
    Next cell
   
 Next i
 
   MsgBox Count 'To test

End Sub
 
Upvote 0
VBA Code:
Try this:
Sub test()
Dim ws1 As Worksheet
Dim cell As Range, chkRng As Range, cntRng As Range, box As Range
Dim i As Long, xCount As Long

Set ws1 = ThisWorkbook.Sheets(1)
Set chkRng = ws1.Range("C4:C13")

For Each cell In chkRng
    If cell.Value = "A" Then
        Set cntRng = ws1.Range(Cells(cell.Row, 4), Cells(cell.Row, 10))
            For Each box In cntRng
                If box.Value = "x" Then
                    xCount = xCount + 1
                End If
            Next box
        End If
    Next cell
MsgBox xCount
End Sub
 
Upvote 1
Solution
Thank you very much this works :)
VBA Code:
Try this:
Sub test()
Dim ws1 As Worksheet
Dim cell As Range, chkRng As Range, cntRng As Range, box As Range
Dim i As Long, xCount As Long

Set ws1 = ThisWorkbook.Sheets(1)
Set chkRng = ws1.Range("C4:C13")

For Each cell In chkRng
    If cell.Value = "A" Then
        Set cntRng = ws1.Range(Cells(cell.Row, 4), Cells(cell.Row, 10))
            For Each box In cntRng
                If box.Value = "x" Then
                    xCount = xCount + 1
                End If
            Next box
        End If
    Next cell
MsgBox xCount
End Sub
 
Upvote 0
Sorry this returns 102 but have solution now, thanks
try this change:
VBA Code:
Sub test()


Set ws1 = Sheet1
Set rng = ws1.Range("C4:C13")
Count = 0
For i = 4 To 13
 
   For Each cell In rng
  
      If ws1.Cells(i, 3).Value = "A" Then
    
       Count = Count + Application.WorksheetFunction.CountIf(Range(Cells(i, 4), Cells(i, 10)), "x")
   
      End If
    Next cell
  
 Next i
 
   MsgBox Count 'To test

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,933
Messages
6,175,471
Members
452,646
Latest member
tudou

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