VBA Count

anthonyexcel

Active Member
Joined
Jun 10, 2011
Messages
258
Office Version
  1. 365
Platform
  1. Windows
I have data that I am trying to count, I need to count Count rows that have Q1, Q2,Q3, and Q4 in it. The code below works for 1 variable i.e.(Q1) and it works but I need an update for All of the variables. Count I am looking for is 7. Thanks in advance!

Q1,Q2,Q3,Q4
Q1,Q2,Y1
Q1,Q2,Q3,Y1
Q1,Q2,Q3,Q4,Y1
Q1,Q2,Q3,Q4,Y1
Q1,Q2,Q3,Q4,Y1
Q1,Q2,Q3,Q4,Y1
Q1,Q2,Q3,Q4,Y1
Q1,Q2,Y1
Y1
Q1,Y1
Q1,Y1
Q2
Q1,Q2,Y1
Q1,Q2,Q3,Y1
Q2,Q3,Q4
Q1,Q2,Q3,Q4
Q2,Q3,Q4
Q2,Q3,Q4
Q2,Q3,Q4
Q2,Q3,Q4


VBA Code:
Function CountQuarters(r As Range, quart As String)
Dim d As Double
Dim parts() As String
    For Each xcell In r
        parts = Split(xcell.Value, ",")
            For Each p In parts
                If InStr(p, quart) > 0 Then
                    d = d + 1
                End If
            Next
        Next
        CountQuarters = d
End Function
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
+Fluff 1.xlsm
ABC
1
2Q1,Q2,Q3,Q47
3Q1,Q2,Y1
4Q1,Q2,Q3,Y1
5Q1,Q2,Q3,Q4,Y1
6Q1,Q2,Q3,Q4,Y1
7Q1,Q2,Q3,Q4,Y1
8Q1,Q2,Q3,Q4,Y1
9Q1,Q2,Q3,Q4,Y1
10Q1,Q2,Y1
11Y1
12Q1,Y1
13Q1,Y1
14Q2
15Q1,Q2,Y1
16Q1,Q2,Q3,Y1
17Q2,Q3,Q4
18Q1,Q2,Q3,Q4
19Q2,Q3,Q4
20Q2,Q3,Q4
21Q2,Q3,Q4
22Q2,Q3,Q4
23
Lists
Cell Formulas
RangeFormula
C2C2=COUNTIFS(A:A,"*Q1,Q2,Q3,Q4*")
 
Upvote 0
Yep that works Fluff, thanks for giving me insight!!!!

VBA Code:
Function xquarters(r As Range)
Dim xcell As Range
    For Each xcell In r
        If xcell.Value Like "*Q1,Q2,Q3,Q4*" Then
            i = i + 1
        End If
    Next
    xquarters = i
End Function
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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