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?
 
If it is in a cell, in which cell? on which sheet?
into a cell which is next to highest value
if there are two or more maximum equal values?
This is an exceptional & rare case. even i havent scene such scenario.
99.99% of times the values would be unique. even of they are same the decimal values would be different.
see below screen shot

decimal.PNG


but do you have more ID
yes i have multiple ID's, but the formatting is same as of the screenshots.
only the difference would be the number of rows of data.
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
See if one of these is any use. Both do allow for multiple maximum values in each section but will work just as well if there is only one.

In this first example, the results are tabulated in columns M, N, O, ...
Columns K:L are helper columns & can be hidden if you want.
Formulas in K2:N2 are copied down.
Formula in O2 is copied across and down.
(Green colouring was just to help me identify the results)

Book1
ABCDEFGHIJKLMNOPQR
1COL1COL2COL3COL4COL5COL6COL7COL8COL9
2Data 1output1116Data 16output6   
3output22818Data 250output13output16output17 
4output332023Data 3100output21output23  
5output44        
6output55        
7output66
8Summaryoutput721
9Data 2output821
10output917
11output1019
12output1121
13output1223
14output1350
15output1427
16output1529
17output1650
18output1750
19output1835
20Summaryoutput19342
21Data 3output2039
22output21100
23output2243
24output23100
25Summaryoutput24282
26
Sheet4
Cell Formulas
RangeFormula
K2:K6K2=IFERROR(AGGREGATE(15,6,ROW(A:A)/(A$2:A$100<>""),ROWS(K$2:K2)),"")
L2:L6L2=IF(K2="","",K2+MATCH("Summary",INDEX(B$2:B$100,K2):B$100,0)-2)
M2:M6M2=IF(K2="","",INDEX(A$2:A$100,K2))
N2:N6N2=IF(K2="","",MAX(INDEX(I$2:I$100,K2):INDEX(I$2:I$100,L2)))
O2:R6O2=IF($K2="","",IFERROR(INDEX($E:$E,AGGREGATE(15,6,ROW(INDEX($E$2:$E$100,$K2):INDEX($E$2:$E$100,$L2))/(INDEX($I$2:$I$100,$K2):INDEX($I$2:$I$100,$L2)=$N2),COLUMNS($O2:O2))),""))



In this second example, the results are provided beside the maximum values as suggested.
K1 houses a 0
J2 & K2 copied down.
Column K can be hidden.

Book1
ABCDEFGHIJK
1COL1COL2COL3COL4COL5COL6COL7COL8COL90
2Data 1output11 1
3output22  
4output33  
5output44  
6output55  
7output66output6x
8Summaryoutput721  
9Data 2output821 2
10output917  
11output1019  
12output1121  
13output1223  
14output1350output13 
15output1427  
16output1529  
17output1650output16 
18output1750output17 
19output1835 x
20Summaryoutput19342  
21Data 3output2039 3
22output21100output21 
23output2243  
24output23100output23x
25Summaryoutput24282  
26  
Sheet5
Cell Formulas
RangeFormula
J2:J26J2=IF(OR(I3="",B2<>""),"",IF(I2=MAX(INDEX(I$2:I2,MATCH(MAX(K$2:K2),K$2:K2,0)):INDEX(I2:I$100,MATCH("x",K2:K$100,0))),E2,""))
K2:K26K2=IF(A2<>"",MAX(K$1:K1)+1,IF(B3<>"","x",""))
 
Upvote 0
Hi @Sagar0650,
I put the option with macro.
But for it to work well, your data must be just as you showed it in your image:
1. Header in row 1.
2. A row of Summary within the merged cell of column "A".
3. The data to be extracted is in column "G".
4. The last column has values, in your example it is the "O" column, but it can be any.
5. After column "O" there should be no data, nor cells with spaces, that is, in your example of column P to the right the cells must be empty.
6. The cells in column A must be merged.
7. The results will be 2 columns to the right of the last column, in this example the results will be written in columns Q and R.


1575385403712.png

------------------------------
VBA Code:
Sub get_merged_cells_5()
  Dim c As Range, dic As Object, r As Range
  Dim wMax As Variant, output As Variant
  Dim ini As Long, fin As Long, lc As Long, i As Long, k As Long
 
  lc = Cells(1, Columns.Count).End(xlToLeft).Column
  Range(Cells(2, lc + 2), Cells(Rows.Count, lc + 3)).ClearContents
  k = 2
  Set dic = CreateObject("scripting.dictionary")
 
  For Each c In Range("A2", 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
      ini = r.Cells(1).Row
      fin = r.Cells(1).Row + r.Rows.Count - 2
      wMax = 0
      For i = ini To fin
        If IsNumeric(Cells(i, lc).Value) Then
          If Cells(i, lc).Value > wMax Then
            wMax = Cells(i, lc).Value
            If Cells(i, "G").MergeCells Then
              output = Cells(i, "G").MergeArea.Cells(1).Value
            Else
              output = Cells(i, "G").Value
            End If
          End If
        End If
      Next
      Cells(k, lc + 2).Value = output
      Cells(k, lc + 3).Value = wMax
      k = k + 1
    End If
  Next
  MsgBox "End"
End Sub

----------------------------------------
8. Attach my test file.


----------------------------------------
9. See example below:

Book1
ABFGHNOPQR
1COL1COL2COL6COL7COL8COL14COL15
2Data 1output11output117
317output8101
43output21100
5output44output2539
6output55
7output66
8Summaryoutput736
9Data 2output8101
1017
1119
1221
13output1223
1450
1527
1629
1731
18output1733
1935
20Summary386
21Data 3output2039
22output21100
23output2243
2445
25Summaryoutput24227
26Data 4output2539
27Summaryoutput2639
Data
Cell Formulas
RangeFormula
O8O8=SUM(O2:O7)
O20O20=SUM(O9:O19)
O25O25=SUM(O21:O24)
O27O27=SUM(O26:O26)
 
Upvote 0
it worked:giggle:
you guys are awesome.
apologies if any of my comments bothered you & Thank you so much for help.
 
Upvote 0
No problem ? . Glad to know that it works for you.
Let me know if you have questions.
Thanks for notifying us.
 
Upvote 0
the alternate option suggested, pivot table. i would like to know more about it.
if you can help me get same result using pivot then it would be great.
please share the file with me if possible so that i can analyse & understand the logic behind it.
Thank you for support.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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