title: Append to Text File VBA
body: <p>I need to take values from a selected range to a comma delimited text file and append them. The code below gives me an error at Set TS. Why??   </p>
  
  <pre><code>Sub Wri()
  
  Dim myrng As Range
  Dim Cell As Range
  
  On Error Resume Next
  Set myrng = Application.InputBox("Select range", Type:=8)
  On Error GoTo 0
  
  If myrng Is Nothing Then
      MsgBox "No cells selected"
      Exit Sub
  End If
  
  Const ForReading = 1, ForWriting = 2, ForAppending = 8
  Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
  
  Dim fs, f, TS, s
  Dim cellv As String
  
  Set fs = CreateObject("Scripting.FileSystemObject")
  fs.CreateTextFile "C:\Users\HP\Documents\fil.txt"
  Set f = fs.GetFile("C:\Users\HP\Documents\fil.txt")
  Set TS = f.OpenTextFile(myrng.Value, 8, True, 0)
  
  For Each Cell In myrng
      cellv = Cell.Value
      TS.Write (cellv &amp; Chr(44))
  Next Cell
  
  End Sub
  </code></pre>