Hello!
I am trying to export 5 columns of data. The rows change each time the data is refreshed. I have a named range of "EquipCosts" for the data. Using the below code, exports, but it give me quotes and commas for the blank rows. Is it possible to export to a csv using a named range? If not, how can I just export the rows that are filled starting with A7? Thanks much for your help!
Sub exportRangeToCSVFile()
Dim myCSVFileName As String
Dim myWB As Workbook
Dim rngToSave As Range
Dim fNum As Integer
Dim csvVal As String
Set myWB = ThisWorkbook
myCSVFileName = "K:\Equipment" & "" & "CSV-Exported-File-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
csvVal = ""
fNum = FreeFile
Set rngToSave = Range("A7:E500")
Open myCSVFileName For Output As #fNum
For i = 1 To rngToSave.Rows.Count
For j = 1 To rngToSave.Columns.Count
csvVal = csvVal & Chr(34) & rngToSave(i, j).Value & Chr(34) & ","
Next
Print #fNum , Left(csvVal, Len(csvVal) - 2)
csvVal = ""
Next
Close #fileNumber
End Sub
I am trying to export 5 columns of data. The rows change each time the data is refreshed. I have a named range of "EquipCosts" for the data. Using the below code, exports, but it give me quotes and commas for the blank rows. Is it possible to export to a csv using a named range? If not, how can I just export the rows that are filled starting with A7? Thanks much for your help!
Sub exportRangeToCSVFile()
Dim myCSVFileName As String
Dim myWB As Workbook
Dim rngToSave As Range
Dim fNum As Integer
Dim csvVal As String
Set myWB = ThisWorkbook
myCSVFileName = "K:\Equipment" & "" & "CSV-Exported-File-" & VBA.Format(VBA.Now, "dd-MMM-yyyy hh-mm") & ".csv"
csvVal = ""
fNum = FreeFile
Set rngToSave = Range("A7:E500")
Open myCSVFileName For Output As #fNum
For i = 1 To rngToSave.Rows.Count
For j = 1 To rngToSave.Columns.Count
csvVal = csvVal & Chr(34) & rngToSave(i, j).Value & Chr(34) & ","
Next
Print #fNum , Left(csvVal, Len(csvVal) - 2)
csvVal = ""
Next
Close #fileNumber
End Sub