我是VBA新手,嘗試從ws複製到wscsv並將後者另存爲.csv文件。以下是我的子例程。VBA ActiveWorkbook.Saveas運行時錯誤1004
我遇到:
運行時錯誤1004:應用程序未定義
在這一行:
ActiveWorkbook.SaveAs Filename:=savedirectory, FileFormat:=xlCSV, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
csvworkbook = ActiveWorkbook.Name
我的代碼
Sub AddNewWorkbook1(ws As Worksheet)
ws.Activate
MsgBox ("adding new workbook for" & ws.Name)
Dim wscsv As Excel.Workbook
Dim savedirectory As String
Dim currentworkbook As String
Dim csvworkbook As String
currentworkbook = ws.Name
savedirectory = '/Users/Desktop/Magnum/' & currentworkbook
Dim lrow As Long
lrow = Columns("A").End(xlDown).Row
Workbooks.Add
DisplayAlerts = False
ActiveWorkbook.SaveAs (Filename:=savedirectory, FileFormat:=xlCSV, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges)
csvworkbook = ActiveWorkbook.Name
Set wscsv = ActiveWorkbook
MsgBox ("Entering copying")
ws.Range(ws.Cells(2, 1), ws.Cells(lrow, 4)).Copy
wscsv.Sheets(1).Range("A1").PasteSpecial xlPasteValues
ws.Range(ws.Cells(2, "H"), ws.Cells(lrow, "H")).Copy
wscsv.Sheets(1).Range("E1").PasteSpecial xlPasteValues
ws.Range(ws.Cells(2, "E"), ws.Cells(lrow, "E")).Copy
wscsv.Sheets(1).Range("F1").PasteSpecial xlPasteValues
ws.Range(ws.Cells(2, "I"), ws.Cells(lrow, "I")).Copy
wscsv.Sheets(1).Range("G1").PasteSpecial xlPasteValues
lrow = wscsv.Sheets(1).Columns("A").End(xlDown).Row
wscsv.Sheets(1).Range(wscsv.Sheets(1).Cells(2, 1), wscsv.Sheets(1).Cells(lrow, 1)).NumberFormat = "mm/dd/yyyy"
wscsv.Sheets(1).Range("A1").Value = "Date"
wscsv.Sheets(1).Range("B1").Value = "open"
wscsv.Sheets(1).Range("C1").Value = "high"
wscsv.Sheets(1).Range("D1").Value = "low"
wscsv.Sheets(1).Range("E1").Value = "close"
wscsv.Sheets(1).Range("F1").Value = "volume"
wscsv.Sheets(1).Range("G1").Value = "cap"
wscsv.Save
wscsv.Close
MsgBox ("Copying complete")
End Sub
是'currentworkbook'一個'CSV'? http://stackoverflow.com/questions/17173898/how-to-do-a-save-as-in-vba-code-saving-my-current-excel-workbook-with-datesta – scorpion
沒有它沒有。它是一個.xlsx。 –