Solved

Excel VBA Merge Cells

Posted on 2013-01-05
4
1,537 Views
Last Modified: 2013-01-05
Hi

What Excel VBA would I use to do each of the ollowing Excel merges

Merge & Center
Merge Across
Merge Cells
0
Comment
Question by:murbro
  • 2
  • 2
4 Comments
 
LVL 50

Expert Comment

by:teylyn
Comment Utility
Hello,

to find out about commands like these, fire up the macro recorder and use the commands one after the other. Then stop the macro recorder and see the code:

Option Explicit

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("A1:C1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge
    Range("A2:C2").Select
    Selection.Merge True
    Range("A3:C3").Select
    Selection.Merge
    Range("A1:C3").Select
    Selection.UnMerge
End Sub

Open in new window


The macro recorder always uses the Select statement, which you can trim from your final code, like

Sub Macro1()

'Merge and Center
    With Range("A1:C1")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Range("A1:C1").Merge
    
' Merge across
    Range("A2:C2").Merge True

' Merge
    Range("A3:C3").Merge

' Unmerge
    Range("A1:C3").UnMerge
End Sub

Open in new window


cheers, teylyn
0
 

Author Comment

by:murbro
Comment Utility
Hi. Thanks

I had done that and post the question because step 1 and 3 seem to be exactly the same,
0
 
LVL 50

Accepted Solution

by:
teylyn earned 500 total points
Comment Utility
No, they're not the same. The actual merge command is the same, but for the Merge and Center there are a few commands that format the cells and precede the merge command. Hence the merged cell content is centered. For the Merge command alone, the contents will be formatted in the same way as the source cell.

If A1 is right-aligned and you use the Merge only option to merge A1 to C1, then the result will be a right-aligned merged cell. The merge command alone will honor existing cell formatting.

Whereas if A1 is right-aligned and you use the Merge and Center, the result will be a centered merged cell.

cheers, teylyn
0
 

Author Closing Comment

by:murbro
Comment Utility
I see. I had assumed that that first bit would have happened for all the merge types but woke up this morning realising that it might not, so thanks very much for the clarification...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now