loop through merged cells in a Column to get their addresses

Sagar0650

Board Regular
Joined
Nov 25, 2019
Messages
55
Office Version
  1. 2019
Platform
  1. Windows
I have one column. say column A, which has multiple merged cells of different ranges.
for example first cell is merged from A2 to A15 whereas second merged cell ranges from A16 to A115
now i want to run through entire column to get the address of each merged cell.
i have code which helps me to get the address of the only first merge cell.
can anyone help me to run a loop to get these cell address?
 
last column is not always fixed, it can vary from range column M to column P
VBA Code:
LastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
this line i am using to find the last column present.

Column 5 always be E
and yes you got is correct with the msgbox output
like: "output2, output13 and output24"
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Ok, try this please

VBA Code:
Sub get_merged_cells_3()
  Dim c As Range, dic As Object, lc As Long
  Dim r As Range, wMax As Long, wRow As Long, wCad As String
  
  lc = Cells(1, Columns.Count).End(xlToLeft).Column - 1
  Set dic = CreateObject("scripting.dictionary")
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
    If c.MergeCells Then
      If Not dic.exists(c.MergeArea.Address(0, 0)) Then
        dic(c.MergeArea.Address(0, 0)) = Empty
        Set r = c.MergeArea
        wMax = WorksheetFunction.Max(r.Offset(, lc).Resize(r.Rows.Count))
        wRow = WorksheetFunction.Match(wMax, r.Offset(, lc).Resize(r.Rows.Count), 0)
        wCad = wCad & WorksheetFunction.Index(r.Offset(, 4).Resize(r.Rows.Count), wRow) & vbLf
      End If
    End If
  Next
  MsgBox wCad, , "Values from column 'E'"
End Sub
 
Last edited:
Upvote 0
getting this error
" unable to get the match property of the worksheetfunction class"
also tried with application.worksheetfunction.match
but same error
 
Upvote 0
What happens is that you have text in the column where you should only have numbers.
In the example, in column I you should have numbers to find the maximum value.
Please, just for the test, delete the texts or blank spaces.
Also check that indeed the last column is column I. (just to continue with the example)
 
Upvote 0
My test data

Book1
ABCDEFGHIJK
1COL1COL2COL3COL4COL5COL6COL7COL8COL9
2Data 1output11
3output22
4output33
5output44
6output55
7output66
8output77
9Data 2output8101
10output917
11output1019
12output1121
13output1223
14output1350
15output1427
16output1529
17output1631
18output1733
19output1835
20output1937
21Data 3output2039
22output21100
23output2243
24output2345
25output2447
26
Sheet


The code a little compacted:
VBA Code:
Sub get_merged_cells_4()
  Dim c As Range, dic As Object, lc As Long
  Dim r As Range, wMax As Long, wRow As Long, wCad As String
 
  lc = Cells(1, Columns.Count).End(xlToLeft).Column - 1
  Set dic = CreateObject("scripting.dictionary")
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
    If c.MergeCells And Not dic.exists(c.MergeArea.Address(0, 0)) Then
      dic(c.MergeArea.Address(0, 0)) = Empty
      Set r = c.MergeArea
      wMax = WorksheetFunction.Max(r.Offset(, lc).Resize(r.Rows.Count))
      wRow = WorksheetFunction.Match(wMax, r.Offset(, lc).Resize(r.Rows.Count), 0)
      wCad = wCad & WorksheetFunction.Index(r.Offset(, 4).Resize(r.Rows.Count), wRow) & vbLf
    End If
  Next
  MsgBox wCad, , "Values from column ""E""    "
End Sub

Result:

1574987530098.png
 
Upvote 0
If you have cells with decimals, then change wMax as Long to wMax as Double:


VBA Code:
Sub get_merged_cells_4()
  Dim c As Range, dic As Object, lc As Long
  Dim r As Range, wMax As Double, wRow As Long, wCad As String
 
  lc = Cells(1, Columns.Count).End(xlToLeft).Column - 1
  Set dic = CreateObject("scripting.dictionary")
  For Each c In Range("A1", Range("A" & Rows.Count).End(xlUp))
    If c.MergeCells And Not dic.exists(c.MergeArea.Address(0, 0)) Then
      dic(c.MergeArea.Address(0, 0)) = Empty
      Set r = c.MergeArea
      wMax = WorksheetFunction.Max(r.Offset(, lc).Resize(r.Rows.Count))
      wRow = WorksheetFunction.Match(wMax, r.Offset(, lc).Resize(r.Rows.Count), 0)
      wCad = wCad & WorksheetFunction.Index(r.Offset(, 4).Resize(r.Rows.Count), wRow) & vbLf
    End If
  Next
  MsgBox wCad, , "Values from column ""E""    "
End Sub

Note: That is the importance of putting real examples.
 
Upvote 0
that is the problem where i am not sure if i can share the real example with you.let me see on that.
thank you very much for your efforts.
Did you try my last macro with the sample data?
 
Upvote 0
that is the problem where i am not sure if i can share the real example with you.let me see on that.
thank you very much for your efforts.
maybe real data no. but information if the cells have texts, or decimals or empties or spaces or formulas. if the sheet is protected or hidden, if the rows are hidden, if the columns are hidden. That information would help since we can't see what you see.
 
Upvote 0
i'll share the exact information with you in coming couple of days.
& this time i'll try to include as much detail as possible so that it would be easy for you to understand the real scenario.
until that time thank you so much for working on the code.
have a nice weekend ahead?
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,653
Members
452,992
Latest member
TokugawaIesuma

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