title: How to create a CSV file if it doesn't exist?
body: <p>I'm trying to get excel to check for the existence of a CSV file, and if it doesn't exist, create it.  I've tried doing this several ways, but I can't seem to get anything working.  Here's my code so far.</p>
  
  <pre><code>If Dir("C:\Users\" &amp; sUserName &amp; "\Desktop\DataDump\") = "" Then
                  MkDir "C:\Users\" &amp; sUserName &amp; "\Desktop\DataDump\"
                  Dim ws As Worksheet
                  For Each ws In ActiveWorkbook.Worksheets
                      If ws.Name = MyCell.Value Then
                          ws.SaveAs "C:\Users\" &amp; sUserName &amp; "\Desktop\DataDump\DataDump.csv", xlCSV
                          Workbooks.Open Filename:="C:\Users\" &amp; sUserName &amp; "\Desktop\DataDump\DataDump.csv"
                          Cells.Select
                          Selection.Delete
                          ActiveWorkbook.Close True
  
                          LastRowCusip = ActiveSheet.Cells(ActiveSheet.Rows.Count, "BW").End(xlUp).Row
                          Set Rng = Range("BG5:BW" &amp; LastRowCusip)
                          Workbooks.Open Filename:="C:\Users\" &amp; sUserName &amp; "\Desktop\DataDump\DataDump.csv"
                          LR = Cells(Rows.Count, "A").End(xlUp).Row + 3 'last used row
                          Rng.Copy Range("A" &amp; LR)
                          ActiveWorkbook.Close True
  
                      End If
                  Next
              End If
  </code></pre>
  
  <p>As soon as I get to this line:</p>
  
  <p>Workbooks.Open Filename:="C:\Users\" &amp; sUserName &amp; "\Desktop\DataDump\DataDump.csv"</p>
  
  <p>Excel shuts down!!</p>
  
  <p>All I want to do is test for the existence of a CSV file and it it doesn't exist, create it on a user's desktop, based on each user's sUserName, which comes from here.</p>
  
  <pre><code>sUserName = Environ$("username")
  </code></pre>
  
  <p>Can someone please help me get this working?
  Thanks!!!</p>