I have the below VBA that allows me to export a CSV file from a defined range. However, there are 2 issues here that I need your help to resolve:
1) When I export using this code, the exported CSV file includes empty/blank fields from the defined range. I believe the problem is that the .csv is taking the cells that have a formula or are within the range and evaluates them as empty strings.
2) When I export the file, zero values (where the formula is not returning any values) are being shown as zero "0" in the CSV file. I'd like these fields to be completely blank.
In other words, I want all empty areas to be shown as blank fields.
Really appreciate your help.
1) When I export using this code, the exported CSV file includes empty/blank fields from the defined range. I believe the problem is that the .csv is taking the cells that have a formula or are within the range and evaluates them as empty strings.
2) When I export the file, zero values (where the formula is not returning any values) are being shown as zero "0" in the CSV file. I'd like these fields to be completely blank.
In other words, I want all empty areas to be shown as blank fields.
Really appreciate your help.
VBA Code:
Sub saveRangeToCSV()
Dim myCSVFileName As String
Dim myWB As Workbook
Dim tempWB As Workbook
Dim rngToSave As Range
Application.DisplayAlerts = False
On Error GoTo err
Set myWB = ThisWorkbook
myCSVFileName = myWB.Path & "\" & "CSV-Dataloader-Import-File-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
Set rngToSave = Range("A3:AE2000")
rngToSave.Copy
Set tempWB = Application.Workbooks.Add(1)
With tempWB
.Sheets(1).Range("A1").PasteSpecial xlPasteValues
.SaveAs Filename:=myCSVFileName, FileFormat:=xlCSVUTF8, CreateBackup:=False
.Close
End With
err:
Application.DisplayAlerts = True
End Sub