我寫了一個vbscript來將xlsx文件的特定範圍保存到csv文件。 我想選擇不相鄰的不同列。每列的範圍應包含其內容的最後一行(範圍("F6").End(xlToRight)
)。
我的代碼:vbscript在範圍命令的xlsx文件中選擇不同的行
Public Sub xlsToCsv()
Const WorkingDir = "C:\"
Const xlCSV = 6
Dim fso, SaveName, myFile
Dim objExcel, objWorkbook, sheet
myFile = "test.xlsx"
SaveName = "test.csv"
With CreateObject("Scripting.FilesystemObject")
If Not .FileExists(WorkingDir & myFile) Then
MsgBox "File not found:" & vbCrLf & WorkingDir & myFile, vbInformation, "Script Cancelled"
WScript.Quit
End If
End With
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.DisplayAlerts = False
Set objWorkbook = objExcel.Workbooks.Open(WorkingDir & myFile)
With objWorkbook.Sheets(1)
.Range("D87", .Range("D87").End(-4121)).Copy
objWorkbook.Sheets.Add().paste
.Range("E87", .Range("E87").End(-4121)).Copy
End With
set sheet = objWorkbook.Sheets.Add()
sheet.paste
objWorkbook.SaveAs WorkingDir & SaveName, 23
objWorkbook.Saved = true
objWorkbook.Close
Set objWorkbook = Nothing
Set objExcel = Nothing
Set fso = Nothing
Set myFolder = Nothing
End Sub
call xlsToCsv()
我得到第18行代碼800A000D與 '範圍' 一類型的衝突。
我該如何做這項工作?
當我保存它作爲.vbs錯誤在l ine 3發生:指示結束expectet – nolags
您聲明您保存爲.csv,上面代碼中的第3行也只是一個聲明而不會失敗。在哪一行失敗的代碼是什麼? – QHarr
Dim WorkingDir As String – nolags