Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write codes for merging cells (& wrap text) in Excel?

Status
Not open for further replies.

Doraemon

IS-IT--Management
Sep 12, 2003
31
HK
As I am trying to open an Excel for reporting from the Access database, I would like to set the format Excel using VBA.

I would like to know specifically what is the coding for mering cells(e.g. D3, D4, D5) into one and then check the "wrap text" property for it.

Thank you for help in advance.
 
Range("D3:F3").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
Selection.Merge
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = True
End With
 
Sorry the above is not very clear - you can set whichever range of cells you want to merge in the first line of the code above, and then use just half of the following code setting whatever characteristics you want!
 
another way to do this.

Range("d3:d5").MergeCells = True
Range("d3:d5").WrapText = True

if you wish to test for above use this.

Sub test()
Dim x As Range

Set x = Range("d3:d5")

If x.MergeCells = False Then
x.MergeCells = True
Else
MsgBox "cells already merged", vbInformation
End If
If x.WrapText = False Then
x.WrapText = True
Else
MsgBox "cells already wrapped", vbInformation
End If

End Sub


Thanks Rob.[yoda]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top