title: Export worksheet to CSV
body: <p>I want to export a worksheet to be stored as a CSV, so that we can refer back to and double check the data set. The code I have output the entire sheet in column A, I want the worksheet to output as it is in the worksheet. Can anyone help me?</p>
  
  <p>It is worth noting thaat both the width and length of the table are variable, hence the counters.</p>
  
  <pre><code>Sub DataSets()
  
  Dim shRefData As Worksheet, shInput As Worksheet
  Set shRefData = ThisWorkbook.Sheets("ReferenceData")
  Dim w, x, y, z As Integer
  Dim filepath As String
  
  filepath = ThisWorkbook.Path + "\"
  w = 3
  z = 1
  x = 1
  
  Open ThisWorkbook.Path + "\" + "Dataset" &amp; Format(Now, "DDMMYY") &amp; ".csv" For Output As #1
  
  Do While shRefData.Cells(w, 1) &lt;&gt; ""
      w = w + 1
  Loop
  Do While shRefData.Cells(3, z) &lt;&gt; ""
      z = z + 1
  Loop
  
  For x = 1 To w
      For y = 1 To z
          Print #1, Cells(x, y)
          y = y + 1
      Next
      x = x + 1
  Next
  
  Close #1
  
  End Sub
  </code></pre>