Export Sheet1 as CSV without trailing commas or empty rows that are just commas at the end of the CSV file

SWAY14

New Member
Joined
Jul 27, 2022
Messages
15
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
  2. MacOS
Hello Everyone,
First time user here. I am looking for help with exporting sheet 1 as a CSV file but also getting ride of the empty rows that represented by a bunch of commas when I open the csv file with textedit or notepad.
I have tried numerous online codes but none seem to do the trick.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi and welcome to MrExcel!

Assuming the data starts in cell A1.
The csv file is saved in the same folder where you have the excel with the macro.

Try this:
VBA Code:
Sub Export_CSV()
  Dim h1 As Worksheet
  Dim fPath As String, fName As String, cad As String
  Dim lr As Long, lc As Long, i As Long
  Dim c As Range

  Set h1 = Sheets("Sheet1")
  fPath = ActiveWorkbook.Path & "\"
  If Left(fPath, 1) <> "\" Then fPath = fPath & "\"
  fName = "filename.csv"
  '
  lr = h1.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  lc = h1.Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
  Open fPath & fName For Output As #1
  For i = 1 To lr
    If WorksheetFunction.CountA(h1.Range(h1.Cells(i, "A"), h1.Cells(i, lc))) > 0 Then
      For Each c In h1.Range(h1.Cells(i, "A"), h1.Cells(i, lc))
        cad = cad & c.Value & ","
      Next
      If cad <> "" Then cad = Left(cad, Len(cad) - 1)
      Print #1, cad
      cad = Empty
    End If
  Next
  Close #1
End Sub
 
Upvote 0
Hi and welcome to MrExcel!

Assuming the data starts in cell A1.
The csv file is saved in the same folder where you have the excel with the macro.

Try this:
VBA Code:
Sub Export_CSV()
  Dim h1 As Worksheet
  Dim fPath As String, fName As String, cad As String
  Dim lr As Long, lc As Long, i As Long
  Dim c As Range

  Set h1 = Sheets("Sheet1")
  fPath = ActiveWorkbook.Path & "\"
  If Left(fPath, 1) <> "\" Then fPath = fPath & "\"
  fName = "filename.csv"
  '
  lr = h1.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  lc = h1.Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
  Open fPath & fName For Output As #1
  For i = 1 To lr
    If WorksheetFunction.CountA(h1.Range(h1.Cells(i, "A"), h1.Cells(i, lc))) > 0 Then
      For Each c In h1.Range(h1.Cells(i, "A"), h1.Cells(i, lc))
        cad = cad & c.Value & ","
      Next
      If cad <> "" Then cad = Left(cad, Len(cad) - 1)
      Print #1, cad
      cad = Empty
    End If
  Next
  Close #1
End Sub
Thank you very much for your help I finally figured it out
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,520
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top