Count Rows Until Cell Value or Cell Value Changes

STIguy

New Member
Joined
Nov 5, 2014
Messages
26
Experts,

I'm stuck on a custom sort. I have converted the data into a tiered format, but now I would like to go through the "Level" column and determine the number of Level 3 entries in each group. For the following data I would like to return the number of rows in each Level 3 group. A named integer would be perfect.

L2item Group 1 - 5 rows of Level 3 items
L2item Group 2 - 4 rows of Level 3 items
L2item Group 3 - 3 rows of Level 3 items

Any advice is greatly appreciated!

Data set:

LevelL2 ItemPNDescription
1 1000Truck
21101Apple
31501Dog
31502Cat
31503Snake
31504Shark
31506Tiger
22102Orange
32601Spider
32602Zebra
32
32
23103Banana
33701Fish
33703Pig
33704Cougar

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I was able to accomplish this by applying Subtotals (Data Tab, Outline Section, Subtotal) to your information two times. You must ensure that you check the box to not replace existing subtotals. Here's what I was able to get.
LevelL2 ItemPNDescription
1 1000Truck
1 Count1
21101Apple
2 Count1
31501Dog
31502Cat
31503Snake
31504Shark
31506Tiger
3 Count5
61 Count
22102Orange
2 Count1
32601Spider
32602Zebra
32
32
3 Count4
52 Count
23103Banana
2 Count1
33701Fish
33703Pig
33704Cougar
3 Count3
43 Count
16Grand Count

<tbody>
</tbody><colgroup><col span="5"></colgroup>
 
Upvote 0
Try this macro. I am assuming that your data is located to columns A to D. Because cell B2 is blank, the first count will result in a zero.
Code:
Sub CountRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("B1:B" & LastRow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range _
        ("B1:B" & LastRow), Unique:=True
    Set rngUniques = Range("B2:B" & LastRow).SpecialCells(xlCellTypeVisible)
    If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
    Dim fRow As Long
    Dim lRow As Long
    Dim strFirst As Range, strLast As Range
    Dim rng As Range
    Dim counter As Long
    For Each rng In rngUniques
        Set strFirst = Range("B2:B" & LastRow).Find(What:=rng, _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
        If Not strFirst Is Nothing Then
            fRow = strFirst.Row
            Set strLast = Range("B2:B" & LastRow).Find(What:=strFirst, _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlPrevious, _
                                MatchCase:=False)
            If Not strLast Is Nothing Then
                lRow = strLast.Row
            End If
        End If
        counter = Application.WorksheetFunction.CountIf(Range(Cells(fRow, "A"), Cells(lRow, "A")), "3")
        MsgBox ("L2Item Group " & rng & " - " & counter & " rows of Level 3 Items")
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Pecan,

Thanks for the quick reply, but I'm looking to do this in VBA because I would like to apply some code once I obtain the row counts. I was thinking of a format something like the code below. I'll need to think of a way to skip to the next range of 3 because the way I have it written currently it will loop through each row.

Code:
Sub SortAssemblyTable()

    Application.ScreenUpdating = False
    
'Range will be variable later
    Dim rng As Range
        Set rng = Range("B6:M25")
    
    Dim rw As Range
    Dim cll As Range
    Dim Level3Count As Long
    
    For Each rw In rng.Rows
        For Each cll In rw.Columns


            If cll.Column = rng.Columns(1).Column And cll.Value = 3 Then
               
                Level3Count = 1
                Do Until cll.Column = rng.Columns(1).Column And cll.Value = 2
                    
'Enter some code here for rows.count or countif?


                Loop
                
                'Apply other code based on number of level 3 rows.
                
            End If


        Next cll
    Next rw
End Sub
 
Upvote 0
mumps - just saw your post. This is great! I need to figure out how to incorporate it into the existing code i've got. Thanks!
 
Upvote 0
mumps - could you explain this section of code:

Code:
Range("B1:B" & lastRow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range _        ("B1:B" & lastRow), Unique:=True
    Set rngUniques = Range("B2:B" & lastRow).SpecialCells(xlCellTypeVisible)
    If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData

So from my understanding you are filtering the level 2 items to determine how many ranges there based on unique values. However it looks like you are essentially defining three different rngUniques to be analyzed in the FOR loop. How is this possible???
 
Upvote 0
That section of code looks in column B and creates a range composed of the unique values, in this case 1, 2 and 3. The rest of the code finds the first and last rows of each group of unique values and uses those rows to count the 3's in column A in the corresponding range. Does this make sense?
 
Upvote 0
Yea, that makes sense. I'm trying to understand where this range is stored because isn't filtering in place typically destructive?
 
Upvote 0
The list of unique values is stored in the variable rngUniques. Once the variable is created, any changes made to the sheet don't affect the contents of the variable.
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,643
Members
449,093
Latest member
Ahmad123098

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