Encountered below error in my vba code.
Run-time error '1004': Copy Method of Worksheet Class failed
Public Sub ExportAsCSV(savePath)
Set ws = Workbooks("OND Estimator").Worksheets("port") 'Sheet to export as CSV
Set wb = Application.Workbooks.Add
ws.Copy Before:=wb.Worksheets(wb.Worksheets.Count)
Application.DisplayAlerts = False 'Possibly overwrite without asking
wb.SaveAs Filename:=savePath, FileFormat:=xlCSV
Application.DisplayAlerts = True
wb.Close savechanges:=False
End Sub
try
Just copy the worksheet to no location. This creates a new active workbook with a single worksheet (a copy of the original) ready to be saved as xlCSV.
I found out the issue on this! I didn't realize hiding the sheets will cause the copy method to not work correctly...
...so what I did is that I revealed the sheet before copying and then hid again once the copy is done.
Thank you guys for your helping me!