I am new to VBA and trying to make a copy from ws to wscsv and save the latter as .csv file. The following is my sub routine.
I am encountering:
run-time error 1004: Application undefined
at this line:
ActiveWorkbook.SaveAs Filename:=savedirectory, FileFormat:=xlCSV, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
csvworkbook = ActiveWorkbook.Name
My Code
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
Could you try this way? It worked for me.