0

I'm trying to develop a code that will allow me to loop through a range and create a total count of rows that have 2 criteria.

For the example below I want to loop through rows 2-12 and increase the counter each time a row has the words "Pway" & "T-4". Then place the result in cell G2. I've played around with loops before but never with two variables.

Can anybody help?

enter image description here

4
  • 2
    You can do this with formulas, do you need it to be vba? Commented Apr 22, 2021 at 12:37
  • I'm open to all options at the moment. Commented Apr 22, 2021 at 12:38
  • If AnswerToAboveComment = "Yes" Then you can nest 2 IF...Then statements to check for 1 criteria and if met, then check the 2nd, if met increase a counter variable. Commented Apr 22, 2021 at 12:39
  • 3
    Basically: =COUNTIFS(A:A,"Pway",D:D,"T-4") Commented Apr 22, 2021 at 12:40

2 Answers 2

2

Please, try the VBA solution, too:

Sub countTwoCriteria()
 Dim sh As Worksheet, arr, lastR As Long, i As Long, count As Long
 
 Set sh = ActiveSheet
 lastR = sh.Range("A" & sh.rows.count).End(xlUp).row
 
 arr = sh.Range("A2:D" & lastR).value
 
 For i = 1 To UBound(arr)
    If UCase(arr(i, 1) & arr(i, 4)) = UCase("Pway" & "T-4") Then count = count + 1
 Next i
 If count > 0 Then sh.Range("G2").value = count
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

Add the following formula in G2 and pull it down

=COUNTIFS($A$2:A2,"Pway",$D$2:D2,"T-4")

To get the following result:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.