Running VBA macro to add COUNTIFS

jack29

New Member
Joined
Apr 28, 2021
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi ,

I have multiple workbooks with 100's to 200's sheets in which I want to count number of entries of A2:A10000 range has, if the cell C2:C10000 has some value then only count should be considered else Not to be considered. This value should be stored in V2 cell of each sheet
Similarly, I want in cell W2, of each sheet I want to count number of entries of A2:A10000 range has, if the cell C2:C10000 has some No-Value then only count should be considered else Not to be considered.

Also, if possible in first sheet, I wanted consolidated grand total of cells value of V2 and W2 across all the sheets.

I tried below VBA code with no luck it prints 0 under V2 and W2 cells. any help is much appreciated.

Sub test()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

With ws
ws.Range("V1").Value = "Rules with Action"
ws.Range("W1").Value = "Rules without Action"
ws.Range("V2") = WorksheetFunction.CountIfs(Range("F2:F5000"), " <> ", Range("M2:M5000"), " <> ")
ws.Range("W2") = WorksheetFunction.CountIfs(Range("F2:F5000"), " <> ", Range("M2:M5000"), "")
End With

Next ws

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this:
VBA Code:
Sub test()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

With ws
ws.Range("V1").Value = "Rules with Action"
ws.Range("W1").Value = "Rules without Action"
ws.Range("V2") = WorksheetFunction.CountIfs(ws.Range("F2:F5000"), " <> ", ws.Range("M2:M5000"), " <> ")
ws.Range("W2") = WorksheetFunction.CountIfs(ws.Range("F2:F5000"), " <> ", ws.Range("M2:M5000"), "")
End With

Next ws

End Sub

You need to reference the ws in the worksheetfunction too.
 
Upvote 0
Hi @Max1616 , I have tried this still it displays zero. could there any other issue. I have attached screenshots.
 

Attachments

  • 2.jpg
    2.jpg
    68.6 KB · Views: 11
  • 1.jpg
    1.jpg
    133.6 KB · Views: 11
Upvote 0
ah, I think I figured out the issue: It looks like there's spaces before & after the inequalities causing the error. This is working for me when I test it in Excel:
VBA Code:
Sub test()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

With ws
ws.Range("V1").Value = "Rules with Action"
ws.Range("W1").Value = "Rules without Action"
ws.Range("V2") = WorksheetFunction.CountIfs(ws.Range("F2:F5000"), "<>", ws.Range("M2:M5000"), "<>")
ws.Range("W2") = WorksheetFunction.CountIfs(ws.Range("F2:F5000"), "<>", ws.Range("M2:M5000"), "=")
End With

Next ws

End Sub
 
Upvote 0
Solution
It's working now, that space was culprit. Even without addition of = (equal to ) sign it's working now, would that equal to necessary or we can leave it blank?

VBA Code:
Sub test()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

With ws
ws.Range("V1").Value = "Rules with Action"
ws.Range("W1").Value = "Rules without Action"
ws.Range("V2") = WorksheetFunction.CountIfs(ws.Range("F2:F5000"), "<>", ws.Range("M2:M5000"), "<>")
ws.Range("W2") = WorksheetFunction.CountIfs(ws.Range("F2:F5000"), "<>", ws.Range("M2:M5000"), "")

'ws.Range("W2") = WorksheetFunction.CountIfs(ws.Range("F2:F5000"), "<>", ws.Range("M2:M5000"), "=")

End With

Next ws

End Sub



Thank you @Max1616
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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