title: Copy/Paste Visible(filtered) Cells to Text (.txt) File
body: <p>I have looked all over the forums and could not find specific code to do exactly this (only pieces of code that probably put together would do what I want).</p>
  
  <p>Would it be possible to do the following:</p>
  
  <ul>
  <li>Filter Column B for "R/R"</li>
  <li>Copy <strong>Visible</strong> cells  in Column P of Sheets("Test1")  -- this sheet pulls its data from an external access query and is setup as a table (not sure if this matters)</li>
  <li>Paste as is to a text file in a specific destination 
  (By as is I mean how it would look if you manually copied and pasted the cell range into a text file) </li>
  </ul>
  
  <p>I wanted to do this with the Write or Print and not simply copy to the clipboard and paste.</p>
  
  <p>Please see below for my code. It filters and copies/pastes to the desired text file however it stops at the first filtered cell, i.e. there are 5 rows with "R/R" in column B (571,4213,4510,5191,5192), but it only pastes cell P571.</p>
  
  <p>Sub abc()</p>
  
  <pre><code>Sheets("Test1").ListObjects("Table_Query_from_MS_Access_Database").Range. _
      AutoFilter Field:=2, Criteria1:="R/R"
  
  LastRow = Sheets("Test1").Range("P" &amp; Rows.Count).End(xlUp).Row
  
  Dim filename As String, lineText As String
  Dim myrng As Range, i, j
  
  filename = "C:\Users\bob\Desktop\output.txt"
  
  Open filename For Output As #1
  
  Set myrng = Sheets("Test1").Range("P2:P" &amp; LastRow).SpecialCells(xlCellTypeVisible)
  
  For i = 1 To myrng.Rows.Count
      For j = 1 To myrng.Columns.Count
          lineText = IIf(j = 1, "", lineText &amp; ",") &amp; myrng.Cells(i, j)
      Next j
      Print #1, lineText
  Next i
  
  Close #1
  </code></pre>
  
  <p>End Sub</p>
  
  <p><strong>EDIT: The code provided by the user initially worked, but there appears to be a bug.
  Whenever there is "R/R" in one row after the other (e.g. Cell B122 &amp; B123), it pastes the data from Cell P122 &amp; P123 to the text file one after the other with a comma instead of moving it to the next line in the text file which is what I wanted. 
  I want it pasted into a text file like this below (PLEASE IGNORE the dashes "-", I needed to place them to put the # on another line in this thread):</strong></p>
  
  <ul>
  <li>1234564789</li>
  <li>46546546489</li>
  <li>134123465465</li>
  <li>7897897897</li>
  <li>465465654</li>
  <li>789789645</li>
  <li>87978978879</li>
  <li>465465465</li>
  </ul>
  
  <p>However, it is pasting it like this with a comma on one of the lines and placing it beside another number:</p>
  
  <ul>
  <li>1234564789</li>
  <li>46546546489</li>
  <li>134123465465</li>
  <li>7897897897</li>
  <li>465465654, 789789645</li>
  <li>87978978879</li>
  <li>465465465</li>
  </ul>