How to justify in merged cells

B

Bill Murphy

I have merged 5 cells horizontally with:

objSht.Range("A1:E1").Merge
objSht.Cells(1, 1) = "My Report Title"

How can I center the report title in the merged cells?

Bill
 
J

JE McGimpsey

I detest merging, since it screws up selections, formatting, sorting,
etc. I'd suggest instead that you center across A1:E1. Something like
this:

Dim rOldSelection As Range

Application.ScreenUpdating = False
Set rOldSelection = Selection
Application.Goto objSheet.Range("A1:E1")
With Selection
.Item(1).Value = "My Title"
.HorizontalAlignment = xlCenterAcrossSelection
End With
Application.Goto rOldSelection
Application.ScreenUpdating = True

It's one of the few times I recommend using Select.
 
G

Greg Wilson

Try:

With objSht.Range("A1:E1")
.MergeCells = True
.Value = "My Report Title"
.HorizontalAlignment = xlHAlignCenter
End With

Regards,
Greg
 
P

Peter T

It's one of the few times I recommend using Select.

Is it necessary to Select? This worked for me:

Sub test()
Dim objSheet As Worksheet
Set objSheet = Worksheets("Sheet3")
With objSheet.Range("A1:E1")
..Clear
..HorizontalAlignment = xlCenterAcrossSelection
..VerticalAlignment = xlVAlignCenter
'.BorderAround 1 ' and whatever
..Cells(1).Value = "My Report Title"
End With
End Sub

Regards,
Peter
 
J

JE McGimpsey

No, it's not.

For me it's a totemic holdover - I had problems with it years ago in a
previous version of XL (I don't recall why, but I recall it as a project
from hell), so ever since I've used a custom function that actually does
the selection.

Time for me to lose that superstition.
 
P

Peter T

For me it's a totemic holdover

I have quite a few of those, never found quite the right
expression to capture so succinctly. I'll use it at the
earliest opportunity :)

Peter
 
Top