Merge a sequence of cells based on the value in a cell.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good day,

I have a VBA which basically works, but I need a code that works on Merging Cells on a sequence. If you look at the VBA you can see the pattern. E3:E4, E5:E6 etc. Because I do not know how many cells that can be merged. I need to have it based on the last cell in Column C that has a Value? If possible I need the same thing for Column P otherwise I will create a separate Macro. In both cases it will begin on Row3. Each merge will only envolve two rows.

Thank you so very much.


VBA Code:
Sub Merge_Column_E()
With Range("E3:E4, E5:E6, E7:E8, E9:E10, E11:E12, E13:E14, E15:E16, E17:E18, E19:E20, E21:E22, E23:E24," & _
"E25:E26, E27:E28, E29:E30, E31:E32, E33:E34, E35:E36, E37:E38, E39:E40, E41:E42, E43:E44, E45:E46, E47:E48, E49:E50," & _
"E51:E52, E53:E54, E55:E56, E57:E58,E59:E60")
    
    .Merge
    .HorizontalAlignment = xlEenter
    .VerticalAlignment = xlEenter

End With
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try this

VBA Code:
Sub Merge_Column_E_P()
  Dim i As Long, lr As Long
  
  For i = 3 To Range("C" & Rows.Count).End(3).Row Step 2
    With Range("E" & i).Resize(2)
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
    With Range("P" & i).Resize(2)
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
  Next
End Sub
 
Upvote 0
Solution
Try this

VBA Code:
Sub Merge_Column_E_P()
  Dim i As Long, lr As Long
 
  For i = 3 To Range("C" & Rows.Count).End(3).Row Step 2
    With Range("E" & i).Resize(2)
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
    With Range("P" & i).Resize(2)
      .Merge
      .HorizontalAlignment = xlCenter
      .VerticalAlignment = xlCenter
    End With
  Next
End Sub
Perfect thank you,
 
Upvote 0

Forum statistics

Threads
1,223,887
Messages
6,175,199
Members
452,617
Latest member
Narendra Babu D

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