VBS脚本用于替换excel文件中的逗号,然后以csv格式保存文件

时间:2016-09-12 16:49:39

标签: excel vba vbscript

我需要在大量excel工作簿中替换逗号,然后将它们保存为csv,我有以下代码,但不知道它为什么不工作:

csv_format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))

Dim oExcel

Set oExcel = CreateObject("Excel.Application")

Dim oBook

Set oBook = oExcel.Workbooks.Open(src_file)

Dim oBookRange
Set oBookRange = oBook.ActiveWorkbook.Sheets(1)

oBookRange.Replace What:=",", Replacement:=" ", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ReplaceFormat:=False

oBook.SaveAs dest_file, csv_format

oBook.Close False
oExcel.Quit

这些行似乎失败了:

set oBookRange = oBook.ActiveWorkbook.Sheets(1)

oBookRange.Replace What:=",", Replacement:=" ", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _ReplaceFormat:=False

1 个答案:

答案 0 :(得分:1)

替换:

Set oBookRange = oBook.ActiveWorkbook.Sheets(1)

使用:

Set oBookRange = oBook.Sheets(1).Cells

可能还有其他问题。