Solved

VBA Excel 2000 - Merge cells

Posted on 2011-02-24
7
738 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:teylyn
teylyn 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:
teylyn 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 50

Expert Comment

by:teylyn
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:teylyn
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

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

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
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 view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now