Merge cells

bobaol

Board Regular
Joined
Jun 3, 2002
Messages
225
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
Hi, I have a question about merging cells in a row. Please consider the grid below, in a jpg image. So, in this situation, I want to merge A1 to K1, merge C2 and D2, merge H2 and I2, merge C3 to G3, merge H3 to K3. The blue cell, "Merge1Begin" is the start of the beginning a merged cell. For example, I want to merge c3 to g3 because it begins with Merge1Begin, then it has Merge2. So, I want C3 to G3 merged because of this pattern. I want to merge cells based on this pattern. any help is appreciated. thanks in advance.
 

Attachments

  • ExcelMerge.JPG
    ExcelMerge.JPG
    52.8 KB · Views: 24

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You simply select each set of cells you want to merge, then click the Merge button. That's very straightforward, so maybe I'm not clear on what your question is.

Also, merging cells is almost always a bad practice that causes problems. You should avoid it.

$scratch.xlsm
ABCDEFGHIJK
1Merge1Begin
2Merge1BeginMerge1Begin
3Merge1BeginMerge1Begin
Merging
 
Upvote 0
Hello, thanks for the response. I'm looking for VBA to do this and automate it. I have 300 tables, and they are all different. yes, i dislike merged cells. this is part of a presentation to make it look pretty.
 
Upvote 0
Note that all your example seem to be merging cells across single rows. In that case, it would be better to use the "Center Across Selection" formatting option instead.
That gives you the same visual effect as merged cells, but without all the issues that merged cells causes.
See: Tom’s Tutorials For Excel: Using Center Across Selection Instead of Merging Cells – Tom Urtis

By the way, do your cells really have the words "Merge1Begin" and "Merge2" in them?
What do you want it to say in them after they have been combined?
 
Upvote 0
Here is some VBA code that will apply the "Center Across Selection" across those cells you want to combine.
It will keep the "Merge1Begin" text and remove the the "Merge2" text.
VBA Code:
Sub MyFormatMacro()

    Dim lr As Long, r As Long
    Dim lc As Long, c As Long
    Dim c1 As Long, c2 As Long
    Dim ms As Boolean
    
    Application.ScreenUpdating = False
    
'   Find last row in on sheet with data
    lr = Range("A1").SpecialCells(xlLastCell).Row
    
'   Loop through all rows
    For r = 1 To lr
'       Find last cell in column with data
        lc = Cells(r, Columns.Count).End(xlToLeft).Column
'       Initialize merge section boolean variable
        ms = False
'       Loop through all columns
        For c = 1 To (lc + 1)
'
            Select Case Cells(r, c)
                Case "Merge1Begin"
'                   See if previous merge section
                    If ms Then
'                       Merge section
                        Range(Cells(r, c1), Cells(r, c2)).HorizontalAlignment = xlCenterAcrossSelection
                    End If
'                   Mark start of merge section
                    ms = True
                    c1 = c
                Case "Merge2"
'                   Increment merge section
                    c2 = c
'                   Clear out word "Merge2"
                    Cells(r, c).ClearContents
                Case Else
'                   Check to see if end of merge section
                    If ms Then
'                       Merge section
                        Range(Cells(r, c1), Cells(r, c2)).HorizontalAlignment = xlCenterAcrossSelection
'                       Reset merge section variable
                        ms = False
                    End If
            End Select
        Next c
    Next r
        
    Application.ScreenUpdating = False
    
    MsgBox "Macro Complete!"
    
End Sub
Not the most elegant code, but gets the job done, I believe.
 
Upvote 0
Thanks for the response. the code did not work. the code deleted all the Merge2, kept Merge1Begin, but the cells did not merge. thanks.
 
Upvote 0
Try this code:


VBA Code:
Option Explicit
Sub mergeCell()
Dim ce As Range, ce1 As Range
For Each ce In ActiveSheet.UsedRange
    If ce.MergeCells = False And ce.Value = "Merge1Begin" Then
        Set ce1 = ce.Offset(, 1)
        Do
            If ce1.Value = "Merge2" Then
                Set ce1 = ce1.Offset(, 1)
            Else
                Set ce1 = ce1.Offset(, -1)
                Exit Do
            End If
        Loop
        Application.DisplayAlerts = False
        With Range(ce, ce1)
            .MergeCells = True
            .HorizontalAlignment = xlCenter
        End With
        Application.DisplayAlerts = True
    End If
Next
End Sub
 
Upvote 0
Solution
The cells weren't supposed to merge. The code uses Center Across Selection so they give the same appearance as merged cells without actually merging them.
 
Upvote 0
Thanks for the response. the code did not work. the code deleted all the Merge2, kept Merge1Begin, but the cells did not merge. thanks.
As Jeff said, they were not supposed to. They were supposed to use the "Center Across Selection" formatting option instead, which gives you the same visual effect as merged cells, but without all the all problems merged cells cause (merged cells are a horrible, horrible thing, and she be avoided whenever possible).

If you read the link I provided, you will understand better.
 
Upvote 0
thanks everyone for the response. bebo's code worked for my needs. As for merging, i mostly agree with the comments. I fully agree when analyzing data or manipulating data. When i get a file, one of the first things i do is uncheck Merge Cells, and uncheck Wrap in the entire sheet. That being said, there is one time when merging is required, such as presenting the data in a view that makes sense to the audience. Usually, this is about formatting a table, then screen-shot it for pasting to PowerPoint. Formatting the data for a presentation is the single only time i use merged cells. i cannot think of any other instance where i would use merged cells. again, thanks everyone!
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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