Count rows per Merge Value

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
just thinking...
although some might say that merge cells is the mortal enemy of macro :) but just the same I would like to throw this not so important question or not
how or can we count rows of a certain column until merge value changes?
say with the given table
Bird row count would be 5
Habitat row count is 8
Mammal is 4 and
Invert is 7

00 GRAMS TEST FILE.xlsx
ABCD
1namegen_specelementsubelement
2Purple sandpiperCalidris maritimaBIRDshorebird
3Long-billed marsh-wrenCistothorous palustrispasserine
4Short-billed marsh-wrenCistothorous platensispasserine
5Baird's sandpiperCalidris bairdiishorebird
6Northern goshawkAccipiter gentilisraptor
7Beach peaLathyrus japonicus var. maritimusHABITATplant
8Beach sumacRhus aromatica var. arenariaupland
9Black-fruit mountain-ricegrassPiptatherum racemosaupland
10Brown-fruited rushJuncus pelocarpuswetland
11Capitate spikerushEleocharis geniculatawetland
12Seaside spurgeChamaesyce polygonifoliaupland
13Rock sandwortMinvartia michauxii michauxiiupland
14Broadleaf sedgeCarex platphyllaupland
15Meadow voleMicrotus pennsylvanicusMAMMALsm_mammal
16Red foxVulpes vulpescanine
17Striped skunkMephitis mephitissm_mammal
18Gray wolfCanis lupuscanine
19Belted kingfisherCeryle alcyonINVERTpasserine
20Mute swanCygnus olorwaterfowl
21Bald eagleHaliaeetus leucocephalusraptor
22Red-shouldered hawkButeo lineatusraptor
23Sharp-shinned hawkAccipiter striatusraptor
24MerlinFalco columbariusraptor
25Rough-legged hawkButeo lagopusraptor
Sheet7
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Something like...

VBA Code:
Option Explicit
Sub RowsPerMerge()
    Dim lr As Long, i As Long, RowsPer As Long, NamePer As String
    lr = Cells(Rows.Count, 3).End(3).Row
    For i = 2 To lr
        RowsPer = Range("C" & i).MergeArea.Rows.Count
        NamePer = Range("C" & i).Value
        If RowsPer > 1 Then
            MsgBox NamePer & " has " & RowsPer & " rows"
            i = i + RowsPer - 1
        End If
    Next i
End Sub
 
Upvote 0
Solution
Something like...

VBA Code:
Option Explicit
Sub RowsPerMerge()
    Dim lr As Long, i As Long, RowsPer As Long, NamePer As String
    lr = Cells(Rows.Count, 3).End(3).Row
    For i = 2 To lr
        RowsPer = Range("C" & i).MergeArea.Rows.Count
        NamePer = Range("C" & i).Value
        If RowsPer > 1 Then
            MsgBox NamePer & " has " & RowsPer & " rows"
            i = i + RowsPer - 1
        End If
    Next i
End Sub
thanks @kevin9999 just stumbled this code but will try to test yours also thanks anyway...
VBA Code:
Sub RowsCount()

    Dim i As Integer, RowCount As Long, LastRow As Long
    Dim SourceSHT As Worksheet

    Set SourceSHT = ActiveSheet
    ActiveSheet.Select
    LastRow = ActiveSheet.UsedRange.Rows.Count + ActiveSheet.UsedRange.Rows(1).Row
    
    For i = 3 To LastRow
        RowCount = Range("A" & i).MergeArea.Rows.Count
        If RowCount > 1 Then
            MsgBox ("Cell [A" & i & "] has " & RowCount & " merged rows")
            i = i + RowCount - 1
        End If
    Next i

End Sub
 
Upvote 0
VBA Code:
Option Explicit
Sub test()
Dim cell As Range
Worksheets("SourceSHT").Activate
For Each cell In Range("C2:C" & Cells(Rows.Count, "D").End(xlUp).Row)
    If cell.Value <> "" Then MsgBox cell.MergeArea(1).Value & " has " & cell.MergeArea.Count & " merged rows"
Next
End Sub
 
Upvote 0
VBA Code:
Option Explicit
Sub test()
Dim cell As Range
Worksheets("SourceSHT").Activate
For Each cell In Range("C2:C" & Cells(Rows.Count, "D").End(xlUp).Row)
    If cell.Value <> "" Then MsgBox cell.MergeArea(1).Value & " has " & cell.MergeArea.Count & " merged rows"
Next
End Sub
@bebo021999 I really like this - the less code the better :)
 
Upvote 0
follow-up if anyone cares, how can I count number of merge entry example with the given table above is 4 (Bird, Habitat, Mammal, Invert). is it possible?
 
Upvote 0
How about
VBA Code:
Sub test()
Dim cell As Range, x As Integer
x = 0
For Each cell In Range("C2:C" & Cells(Rows.Count, "D").End(xlUp).Row)
    If cell.Value <> "" Then
    MsgBox cell.MergeArea(1).Value & " has " & cell.MergeArea.Count & " merged rows"
     x = x + 1
End If
Next
 MsgBox "There are " & x & " Groups !!"
End Sub
 
Upvote 0
How about
VBA Code:
Sub test()
Dim cell As Range, x As Integer
x = 0
For Each cell In Range("C2:C" & Cells(Rows.Count, "D").End(xlUp).Row)
    If cell.Value <> "" Then
    MsgBox cell.MergeArea(1).Value & " has " & cell.MergeArea.Count & " merged rows"
     x = x + 1
End If
Next
 MsgBox "There are " & x & " Groups !!"
End Sub
simplicity is beauty thanks @Michael M
 
Upvote 0

Forum statistics

Threads
1,223,627
Messages
6,173,420
Members
452,514
Latest member
cjkelly15

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