gpt4 book ai didi

excel - 自动打开、更新和保存 Excel 工作簿

转载 作者:行者123 更新时间:2023-12-02 10:01:06 26 4
gpt4 key购买 nike

我想在 Excel 中自动修改列的数字格式。

Set excel = CreateObject("Excel.Application")
Set oWB = excel.Workbooks.Open("E:\Docs\Invoice.csv")

/* Excel Macro starts */
Columns("G:G").Select
Selection.NumberFormat = "m/d/yyyy"
Columns("H:H").Select
Selection.NumberFormat = "0.00"
/* Excel Macro ends */

oWB.save
oWB.Application.Quit

我使用命令行运行这个.vbs。 Excel 文档未更新。
有人可以帮我解决这个问题吗?

提前致谢

最佳答案

上面的代码中缺少的是您没有完全限定 Excel 对象。

vbs 如何理解Columns("G:G") 是什么?

试试这个

Dim objXLApp, objXLWb, objXLWs

Set objXLApp = CreateObject("Excel.Application")
Set objXLWb = objXLApp.Workbooks.Open("E:\Docs\Invoice.csv")

'~~> Working with Sheet1
Set objXLWs = objXLWb.Sheets(1)

With objXLWs
'/* Excel Macro starts */
.Columns("G:G").NumberFormat = "m/d/yyyy"
.Columns("H:H").NumberFormat = "0.00"
'/* Excel Macro ends */
End With

objXLWb.Save
objXLWb.Close (False)

Set objXLWs = Nothing
Set objXLWb = Nothing

objXLApp.Quit
Set objXLApp = Nothing

编辑:我唯一担心的是数字格式不会保留为 CSV 文件。您可能想将其另存为 Excel 文件?

代码

Dim objXLApp, objXLWb, objXLWs

Set objXLApp = CreateObject("Excel.Application")

objXLApp.Visible = True

Set objXLWb = objXLApp.Workbooks.Open("E:\Docs\Invoice.csv")

'~~> Working with Sheet1
Set objXLWs = objXLWb.Sheets(1)

With objXLWs
.Columns("G:G").NumberFormat = "m/d/yyyy"
.Columns("H:H").NumberFormat = "0.00"
End With

'~~> Save as Excel File (xls) to retain format
objXLWb.SaveAs "C:\Sample.xls", 56

'~~> File Formats
'51 = xlOpenXMLWorkbook (without macro's in 2007-2010, xlsx)
'52 = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2010, xlsm)
'50 = xlExcel12 (Excel Binary Workbook in 2007-2010 with or without macro's, xlsb)
'56 = xlExcel8 (97-2003 format in Excel 2007-2010, xls)

objXLWb.Close (False)

Set objXLWs = Nothing
Set objXLWb = Nothing

objXLApp.Quit
Set objXLApp = Nothing

关于excel - 自动打开、更新和保存 Excel 工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12312566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com