[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VBA Excel 2000 - Merge cells

Posted on 2011-02-24
7
Medium Priority
?
771 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
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.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

649 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