• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

VBA Excel 2000 - Merge cells

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
csehz
Asked:
csehz
  • 4
  • 3
2 Solutions
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
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
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
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
 
csehzIT consultantAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
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
 
csehzIT consultantAuthor Commented:
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
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
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
 
csehzIT consultantAuthor Commented:
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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now