title: Export selected rows and columns to CSV-file
body: <p>I want to be able to export a selected range of cells to a .csv file using VBA. What I have come up with so far does the job excellently for cohering selections, but fails misearably when multiple columns are selected.</p>
  
  <p>Here is the code I managed to put together from snippets found on the internet: It also fiddles around with some UI and since my Excel speaks German and I need to have "." as decimal separator instead of "," it tweaks that.</p>
  
  <pre><code>Sub Range_Nach_CSV_()
  Dim vntFileName As Variant
  Dim lngFN As Long
  Dim rngRow As Excel.Range
  Dim rngCell As Excel.Range
  Dim strDelimiter As String
  Dim strText As String
  Dim strTextCell As String
  Dim strTextCelll As String
  Dim bolErsteSpalte As Boolean
  Dim rngColumn As Excel.Range
  Dim wksQuelle As Excel.Worksheet
  Dim continue As Boolean
  
  strDelimiter = vbtab
  
  continue = True
  
  Do While continue = True
  
  vntFileName = Application.GetSaveAsFilename("Test.txt", _
      FileFilter:="TXT-File (*.TXT),*.txt")
  If vntFileName = False Then
      Exit Sub
  End If
  
  If Len(Dir(vntFileName)) &gt; 0 Then
      Dim ans As Integer
      ans = MsgBox("Datei existiert bereits. Überschreiben?", vbYesNo)
      If ans = vbYes Then
          continue = False
      ElseIf ans = vbNo Then
          continue = True
      Else
          continue = False
      End If
  Else
      continue = False
  End If
  
  Loop
  
  Set wksQuelle = ActiveSheet
  
  lngFN = FreeFile
  Open vntFileName For Output As lngFN
  
      For Each rngRow In Selection.Rows
          strText = ""
          bolErsteSpalte = True
  
          For Each rngCell In rngRow.Columns
              strTextCelll = rngCell.Text
              strTextCell = Replace(strTextCelll, ",", ".")
              If bolErsteSpalte Then
                  strText = strTextCell
                  bolErsteSpalte = False
              Else
                  strText = strText &amp; strDelimiter &amp; strTextCell
              End If
          Next
  
      Print #lngFN, strText
  
      Next
  Close lngFN
  
  End Sub
  </code></pre>
  
  <p>As I already mentioned the sub works well with coherent selections and also with multiple selected lines, but fails when it comes to multiple columns.</p>
  
  <p>The current output of the sub can be seen on this here picture:
  <a href="http://i.stack.imgur.com/05CMG.jpg" rel="nofollow">multiple columns failed</a></p>
  
  <p>As one would expect, I want the .csv-file (or respective .txt-file) to look like this:
  <a href="http://i.stack.imgur.com/VcxWC.jpg" rel="nofollow">multiple columns desired output</a></p>
  
  <p>How can I achieve the desired behaviour for the last case?
  And would someone be so kind to include the links as images? If perceived appropriate, of course.</p>