Merge and centre current row with next blank row if value is "Open" in column "A". VBA Program

Akash030193

New Member
Joined
Apr 28, 2019
Messages
22
I want to Merge and centre current row (row 2) with next blank row (row 3) if value is "Open". Similarly for other rows (6-7, 8-9, 10-11 etc.)

Can you please provide any suitable VBA Program?


AB
1Statusremark
2Open
3
4Close
5Close
6Open
7
8Open
9
10Open
11
12Close

<tbody>
</tbody>
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Why would you want to merge cells ?
They can / will cause all sorts of problems in the future with filtering, sorting, etc......but if you must

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("A" & r).Value = "Open" Then
        With Range("A" & r & ":A" & r + 1)
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .Merge
        End With
    End If
Next r
End Sub
 
Upvote 0
Dear Michael M,

Wow... It is working........ Thanks a lot... very very much.:)

FYI. I will no need any further filtering, sorting etc. in future.

Regards
Akash Patel
 
Upvote 0
Ok then....glad to help....and thx for the feedback...(y)
 
Upvote 0
Thanks Michael,

Your program is working well. But further to your macro program. I need your help on below query!
I want to Merge and centre current merged row (row 2-3) with next blank row (row 4) if value is "Open" (merged row 2-3). Similarly for other rows (6-7, 8-9-10-11 etc.).

In short, I need to merge "open" comment with next blank cell even if the cell with "open" valve is merged or not.


Can you please provide any suitable VBA Program?

AB
1Open
2Open (row 2 and 3 are merged)
3Open (row 2 and 3 are merged)
4
5Close
6Open
7
8Open (row 8-9-10 merged)
9Open (row 8-9-10 merged)
10Open (row 8-9-10 merged)
11
12close
13close
14close

<tbody>
</tbody>
Thanks in advance :)
 
Upvote 0
Maybe this....but as mentioned previously, I would avoid mergede cells whenever possible

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("A" & r).Value = "Open" Then
        With Range("A" & r & ":A" & r + 2)
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .Merge
        End With
    End If
Next r
End Sub
 
Upvote 0
Try this

Code:
Sub merge_cell()
    Dim c As Range
    Application.DisplayAlerts = False
    For Each c In Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
        If c(1).Value = "Open" Then
            c.HorizontalAlignment = xlCenter
            c.VerticalAlignment = xlCenter
            c.Merge
        End If
    Next
End Sub
 
Upvote 0
If you have something like this:

<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:81.74px;" /><col style="width:96.95px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >STATUS</td><td >REMARK</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td >Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td >Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td >Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td >Close</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td >Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td >Open</td><td style="font-weight:bold; "> </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td >Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td >Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td >Close</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td >Close</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td >Close</td><td > </td></tr></table>


This is the result:

<table border="1" cellspacing="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:81.74px;" /><col style="width:96.95px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td > </td><td >A</td><td >B</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td >STATUS</td><td >REMARK</td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td rowspan="3" style="text-align:center; ">Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td >Close</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td >Open</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >9</td><td rowspan="3" style="text-align:center; ">Open</td><td style="font-weight:bold; "> </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >10</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >11</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >12</td><td > </td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >13</td><td >Close</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >14</td><td >Close</td><td > </td></tr><tr style="height:19px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >15</td><td >Close</td><td > </td></tr></table>

---
Your data should be in column A and start in cell A2, and should only have the text "Open"


Tell me how do you have your data and where do you have it?
 
Upvote 0
Hi Michael,

Your macro program is suitable to merge 2 merged rows with next blank row if the comment is open. (your command is r+2)

But not suitable for to merge 3 merged rows with next blank row as per the table (merged row 8-9-10 to be merged with 11).

Can you make me a program that can merge "open" comment with next blank cell, even if, the cell with "open" valve is merged with end number of rows, or not.

Let me explain you once again with new example:


ABC
remark
1Openasrow 1-2 already merged
2adrow 1-2 already merged
3afThis row (cell A3) To be merged with 1-2 (Cell A1-A2)
4Openas
5dfThis row (cell A5) To be merged with 4 (Cell A4)
6Closevr
7Openvfrow 7-8-9-10 already merged
8row 7-8-9-10 already merged
9row 7-8-9-10 already merged
10row 7-8-9-10 already merged
11This row (cell A11) To be merged with 7-8-9-10 (Cell A7 to A10)
12Close
13Close
14Close
15Close
16Close
17Close
18Close

<tbody>
</tbody>


you have made your previous programs using r+1, r+2 formulas. And it is working. It can be better if You can make program that is having formula r+n type..........this will help me.
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

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