Solved

Excel VBA Merge Cells

Posted on 2013-01-05
4
1,755 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 50
ID: 38747489
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:Murray Brown
ID: 38747502
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:
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 500 total points
ID: 38747508
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:Murray Brown
ID: 38748120
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

695 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