Set the number format of cells in an Excel file

本文介绍如何使用VBA脚本通过Excel对象模型来格式化外部Excel文件中的单元格格式,包括常规格式、百分比和货币格式。请注意这些操作不适用于QuickTest Professional的数据表。


Use the NumberFormat Excel property


This solution applies to external Excel files, not to the QuickTest Professional data table.


Excel's object methods are not part of QuickTest Professional, therefore, they are not guaranteed to work and are not supported by Mercury Technical Support. Any changes that Microsoft may make to these methods are not the responsibility of Mercury.

Example:
filename = "C:\temp\sample.xls"

Set ExcelObj = CreateObject("Excel.Application")
ExcelObj.Workbooks.Open filename
ExcelObj.Visible = True
Set NewSheet = ExcelObj.Sheets.Item(1)

' Format the Cells to General
'NewSheet.Columns("A").Select
NewSheet.Cells(1,1) = "test"
NewSheet.Cells(1,1).Select
ExcelObj.Selection.NumberFormat = "General"

' Format the cells to Percentage
NewSheet.Cells(1,2) = ".50"
NewSheet.Columns("B").Select
ExcelObj.Selection.NumberFormat = "##%"

' Format the cells to Currency
NewSheet.Cells(1,3) = "6.50"
NewSheet.Columns("C").Select
ExcelObj.Selection.NumberFormat = "$0.00"


ExcelObj.ActiveWorkbook.Save
ExcelObj.Application.Quit
Set ExcelObj = Nothing

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值