Count rows that meet 2 IF conditions

sneeland

New Member
Joined
Jul 31, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I know this shouldn't be fighting me...

Trying to put together o365 VB macro. I have some data. Need to walk each row. Need a count of total rows for each "PRIORITY" (column name) value of 1 thru 4 and the count of each that "Percent Complete" IS NOT "100.00". Want to write these counts on a newly created sheet in the same workbook.

Source data:
1596171081514.png



Desired output:
1596171146643.png


I've spent too much time on this and I know I'm missing one small thing. I'd appreciate any solutions from the group. Thank you so much!!!

Steve
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
WELCOME TO MREXCEL

Try this
Amend first condition if required (see note below)
This is the Excel formula which must work
=COUNTIFS(I:I,"<1",G:G,"1*")

Run from the sheet containing the values to be counted
VBA Code:
Sub CountRows()
    Dim Priority As Range, Percent As Range, p As Long, ws As Worksheet, pCount As Long
    With ActiveSheet
        Set Priority = .Range("G2", .Range("G" & .Rows.Count).End(xlUp))
    End With
    Set Percent = Priority.Offset(, 2)
    Set ws = Sheets.Add
    ws.Cells(1, 2) = "Open"
    For p = 1 To 4
        pCount = WorksheetFunction.CountIfs(Percent, "<1", Priority, p & "*")
        ws.Cells(p + 1, 1).Resize(, 2) = Array("Priority " & p, pCount)
    Next p
End Sub

NOTE
Condition may be incorrect - it depends on the format of the underlying data in percent column

May need to be
Percent, "<100"
or if text
Percent, "<>100.00"

If you cannot get it to work, let me know
 
Last edited:
Upvote 0
You are amazing! I did have to make the suggested change since the cell with "100.00" is being considered as text. Works perfectly! And only 30 lines less than my failed attempt :) I do need to keep it as a script to append to some other subroutines I have running also.

Sub CountRows()
Dim Priority As Range, Percent As Range, p As Long, ws As Worksheet, pCount As Long
With ActiveSheet
Set Priority = .Range("G2", .Range("G" & .Rows.Count).End(xlUp))
End With
Set Percent = Priority.Offset(, 2)
Set ws = Sheets.Add
ws.Cells(1, 2) = "Open"
For p = 1 To 4
pCount = WorksheetFunction.CountIfs(Percent, "<>100.00", Priority, p & "*")
ws.Cells(p + 1, 1).Resize(, 2) = Array("Priority " & p, pCount)
Next p
End Sub


Thank you so much!!

Steve
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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