?
Solved

VBA Excel 2000 - Merge cells

Posted on 2011-02-24
7
Medium Priority
?
763 Views
Last Modified: 2012-06-21
Dear Experts,

In the attached Code I wanted to do a simple task, merging cells A1:B1, after merging A3:G3.

But somehow the macrorecorder created a lot of lines for it, could you advise all of these are mandatory? Or in really VBA language it can be done shorter?

thanks,
Worksheets("Sheet1").Select
    Range("A1:B1").Select
    Range("B1").Activate
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With
    Selection.Merge
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .MergeCells = True
    End With
    Range("A3:G3").Select
    Range("G3").Activate
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .ShrinkToFit = False
        .MergeCells = False
    End With
    Selection.Merge
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .MergeCells = True
    End With

Open in new window

0
Comment
Question by:csehz
[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
  • 4
  • 3
7 Comments
 
LVL 50

Assisted Solution

by:Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 2000 total points
ID: 34968585
Hello,

don't use merged cells. They cause more trouble than they are worth. Use Center across Selection in the Alignment tab of the format dialog instead.

If you insist, the code could be culled to

Worksheets("Sheet1").Select
    Range("A1:B1").Merge
    Range("A3:G3").Merge

Open in new window


cheers, teylyn
0
 
LVL 50

Accepted Solution

by:
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 2000 total points
ID: 34968597
To use Center across Selection in a macro, try

    Range("A1:B1").HorizontalAlignment = xlCenterAcrossSelection
    Range("A3:G3").HorizontalAlignment = xlCenterAcrossSelection

Open in new window


cheers, teylyn
0
 
LVL 1

Author Comment

by:csehz
ID: 34968629
Teylyn thanks very much, you help me so lot of that points are unable to express it. Both are working of course.

In the second version could be done also that the text would be adjusted to Left?

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50
ID: 34968725
In this case you don't need a macro at all. Just put the text in the leftmost cell. If it is too wide for the current cell, it will flow into the next one, unless the cell is formatted to wrap.



0
 
LVL 1

Author Comment

by:csehz
ID: 34968789
Teylyn thanks, basically the need which drived me to this question, that I have a lot of reports to send everyday to people. Its general format is like this around with texts

<<Dear All, the created xxx report>> in cell A1

<<hyperlink pointing to the daily file>> in A3

<<pivot table with some of columns>> starting from A5

My target is to have this sheet created by a macro automatically, so after it can be copied simply to mail and send.
Currently so if the A1 and A3 are not merged as A1:B1 and A3:G3, the mail looks strange a bit because the long hyperlink is grouped to one cell.

But you two lines code anyway perfect to solve it, so just wanted to clear its target.

thanks,
0
 
LVL 50
ID: 34968965
Ah, I understand what you are doing, now.

In this case, merging cells is probably OK. You can use this to merge cells and then left-align the text

    With Range("A1:B1")
        .Merge
        .HorizontalAlignment = xlLeft
    End With
    With Range("A3:G3")
        .Merge
        .HorizontalAlignment = xlLeft
    End With

Open in new window


cheers, teylyn
0
 
LVL 1

Author Comment

by:csehz
ID: 34968988
Yes in that way is perfect, thanks very much
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

777 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