title: Trying to export a range in Excel to CSV with only cells that contain data
body: <p>Good afternoon all,</p>
  
  <p>I am building an excel sheet for my coworkers to use to generate CSV files. I need to get an output with header row and then all the data from the sheet. It will be no more than 40 rows, but could be less rows. I currently have a lot of formulas in the sheet doing the heavy lifting for my coworkers on things like generating usernames/etc from the input data. I need to have them click a button and get the CSV file on the other end. My current issues are as follows.</p>
  
  <p>1.) my CSV contains double quotes on every field, even though commas should not be in the input data. I need to prevent this as the program we are feeding these csv files to does not like the double quotes AT ALL. Yes, I know you can open it in notepad and replace all to remove them but im trying to build a one click solution as some of the folks using this are not very tech savvy.</p>
  
  <p>2.) my macro is exporting all forty rows of data currently. I need it to only export the rows that contain data. Theoretically with the formulas built there should be no "partial" rows, only a full row or a blank row.</p>
  
  <p>3.) When generating the CSV file its not appending a filetype at all, I need it to specify a .txt. filetype if at all possible as again, the program we are feeding these to is very picky.</p>
  
  <pre><code>Sub CommandButton1_Click()
  Dim filename As String
  Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer
  
  filename = InputBox("Please enter file name", "Save as CSV", "CSV_" &amp; Format(Now, "DD_MM_yyyy"))
  myFile = Application.DefaultFilePath &amp; filename
  
  Set rng = Range("A1:J41")
  
  Open myFile For Output As #1
  
      For i = 1 To rng.Rows.Count
          For j = 1 To rng.Columns.Count
  
              cellValue = rng.Cells(i, j).Value
  
                  If j = rng.Columns.Count Then
                      Write #1, cellValue
                          Else
                      Write #1, cellValue,
                  End If
          Next j
      Next i
  
  Close #1
  </code></pre>
  
  <p>End Sub</p>