VBA: SaveAs method creates infinite csv file

88 Views Asked by At

I am looping through several worksheets in a file, copying information to another sheet (called wsCSV, which is stored in ThisWorkbook) and then I want to create a CSV file from this worksheet. After creating the sheet, the loop should continue to the next sheet.

Here is part of the code which should create the CSV:

' Create CSV file
If iControl = 1 Then
    Application.DisplayAlerts = False
    wsCSV.Copy
    ActiveWorkbook.SaveAs Filename:=sDestin & "\" & sShName & ".csv", FileFormat:=xlCSV, CreateBackup:=True
    ActiveWorkbook.Close SaveChanges:=False
    Application.DisplayAlerts = True
End If

Anyway, when I proceed the code, SaveAs method starts creating "infinite" file, till it consumes all my disk space. I don't know how is it possible, as the file has 6 rows and 3 columns, so it should have 1-2 kb. I have to stop the process manually. Any thoughts? I tried this code in another macro and it worked OK.

Thanks!

1

There are 1 best solutions below

0
On

Make sure that your wsCSV worksheet is truly as empty as you think it is. Use Ctrl-End to see what the last used cell is and clean it up if necessary.