VBScript Excel格式化.xlsx文件

时间:2008-09-26 21:31:52

标签: vbscript

基本上我想知道如何使用VBScript设置单元格的中心对齐...

我一直在谷歌搜索它,似乎找不到任何有用的东西。

2 个答案:

答案 0 :(得分:4)

Set excel = CreateObject("Excel.Application")

excel.Workbooks.Add() ' create blank workbook

Set workbook = excel.Workbooks(1)

' set A1 to be centered.
workbook.Sheets(1).Cells(1,1).HorizontalAlignment = -4108 ' xlCenter constant.

workbook.SaveAs("C:\NewFile.xls")

excel.Quit()

set excel = nothing

'If the script errors, it'll give you an orphaned excel process, so be warned.

将其另存为.vbs并使用命令提示符或双击运行它。

答案 1 :(得分:1)

有多种方法可以选择一个细胞或一系列细胞,但以下内容适用于单个细胞。

'Select a Cell Range
Range("D4").Select

'Set the horizontal and vertical alignment
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
End With

Horizo​​ntalAlignment选项是xlLeft,xlRight和xlCenter