title: Create multiple text files for selected data on excel using vba scripting in single click
body: <p>I am using the code below to generate the single file for selected range and considering the first cell in the selected range as file name. Please find the image below for more details<a href="http://i.stack.imgur.com/9TlxX.jpg" rel="nofollow">[This image shows the selected range,Consider K column(Firstline) and N Column( Lastline) to be in one file and other set of 1st and last line in other file ]</a><a href="http://i.stack.imgur.com/rmdDW.jpg" rel="nofollow">this image shows the print file for a single file this is the way m currently using for generating files.I need to create more 30k files so please help me to create more files in single click considering the first and last line as header and footer for the file</a></p>
  
  <pre><code>Private Sub CommandButton1_Click()
  
  Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer, path As String, filename, filename2 As String
  path = "D:\Watchlist-Files\"
  
  filename = Selection.Cells(1, 1).Value
  filename2 = Left(Mid(filename, 32, 99), Len(Mid(filename, 32, 99)) - 2)
  
  myFile = path &amp; filename2
  
  Set rng = Selection
  
  Open myFile For Output As #1
  
  For i = 1 To rng.Rows.Count
      For j = 1 To rng.Columns.Count
  
  cellValue = rng.Cells(i, j).Value
  
  
  If j = rng.Columns.Count Then
      Print #1, cellValue
  Else
      Print #1, cellValue,
  
  End If
  
  
     Next j
  Next i
  
  
  Close #1
  
  End Sub
  </code></pre>