以下代码将C6:C7区域的单元格文字左右及上下居中,
Sub Macro1()
'
' Macro1 Macro
' zcb 记录的宏 2002-6-18
''
Range("C6:C7").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
这段代码是通过录制宏得到的关键是这两行
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
Sub Macro1()
'
' Macro1 Macro
' zcb 记录的宏 2002-6-18
''
Range("C6:C7").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
End Sub
这段代码是通过录制宏得到的关键是这两行
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
Sub Macro2()
'
' Macro2 Macro
' zcb 记录的宏 2002-6-18
''
Range("C6:D7").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
End Sub
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim i As Integer
Dim strFile As String
strFile = "c:\abc.xls"
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets("Sheet1")
xlSheet.Cells(1, 1) = "Col1"
For i = 1 To 10
xlSheet.Cells(i, 2) = "Col2"
Next i
For i = 1 To 10
xlSheet.Cells(i, 3) = "Col3"
Next i
xlSheet.Range("A1:A10").Merge '合并单元格
xlSheet.Range("A1:A10").HorizontalAlignment = xlCenter
xlSheet.Range("A1:A10").VerticalAlignment = xlCenter
xlSheet.Range("B1:B10").HorizontalAlignment = xlRight
xlSheet.Range("B1:B10").VerticalAlignment = xlCenter
xlSheet.Range("C1:C10").HorizontalAlignment = xlLeft
xlSheet.Range("C1:C10").VerticalAlignment = xlCenter
xlSheet.SaveAs strFile
Set xlSheet = Nothing
xlBook.Close
Set xlBook = Nothing
xlApp.QUIT
Set xlApp = Nothing
MsgBox "OK"
End Sub