Solved

VBA Excel 2000 - Merge cells

Posted on 2011-02-24
7
743 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
  • 4
  • 3
7 Comments
 
LVL 50

Assisted Solution

by:Ingeborg Hawighorst
Ingeborg Hawighorst earned 500 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 earned 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
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

Expert Comment

by:Ingeborg Hawighorst
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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 will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

863 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

25 Experts available now in Live!

Get 1:1 Help Now