I have the following code to save as sheet as a CSV and ensuring that the cols are autofitted , but I cannot get the cols in the CSV to be autofitted
The Cols on Sheet "Settlements" on the Excel workbook are all autofitted so I can't understand why the cols the CSV file is not autofitted
It would be appreciated if someone could amend my code so that the cols are autofitted
The Cols on Sheet "Settlements" on the Excel workbook are all autofitted so I can't understand why the cols the CSV file is not autofitted
It would be appreciated if someone could amend my code so that the cols are autofitted
Code:
Sub SaveSheetAsCSVWithAutofit()
Dim ws As Worksheet
Dim newWB As Workbook
Dim savePath As String
' Set the worksheet to be saved as CSV
Set ws = ThisWorkbook.Sheets("Settlements") ' Change the sheet name as needed
' Create a new workbook
Set newWB = Workbooks.Add
' Copy data to new workbook
ws.Cells.Copy newWB.Sheets(1).Range("A1")
' Autofit columns in the new workbook (won't affect CSV file)
newWB.Sheets(1).Cells.EntireColumn.AutoFit
' Specify the save path and filename
savePath = "C:\My Documents\MRTY Settlement.csv" ' Update with your desired path
' Save as CSV file
newWB.SaveAs Filename:=savePath, FileFormat:=xlCSV, CreateBackup:=False
' Close and save changes to the new workbook
newWB.Close SaveChanges:=True
End Sub