title: Extract excel data to text file
body: <p>I have an excel file whose data is from left to right. I want to copy it in a notepad from top to bottom. Let me explain. As shown in the <a href="https://i.stack.imgur.com/MRfQH.png" rel="nofollow noreferrer">excel sheet picture</a>, the excel sheet is divided into layers, Layer 0, Layer 1 and so on.
  <a href="https://i.stack.imgur.com/ywL0r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ywL0r.png" alt="enter image description here" /></a>
  I want the vba code to copy all the data from the first column (Layer 0) up until the 5th column  into notepad. Then start copying the data again from the 6th column (layer 1, the red boundary) up until the end of that layer on a new line. The operation goes on and on. So that it produces a result like <a href="https://i.stack.imgur.com/2yBcy.png" rel="nofollow noreferrer">this</a>. This is what I tried
  <a href="https://i.stack.imgur.com/D0Rcq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D0Rcq.png" alt="enter image description here" /></a></p>
  <pre><code>Sub SaveAsTxtFile()
  Dim FileName As String
  Dim SLine As String
  Dim Deliminator As String
  Dim Lastcol, LastRow, FileNumber As Integer
  'Locationand File Name
  FileName = &quot;/Users/random/Desktop/G_code.Path.txt&quot;
  
  Deliminator = &quot; &quot;
  Lastcol = Sheet1.Cells.SpecialCells(xlCellTypeLastCell).Column
  LastRow = Sheet1.Cells.SpecialCells(xlCellTypeLastCell).Row
  FileNumber = FreeFile
  
  'creating or Overwritting a text file
  Open FileName For Output As FileNumber
  
  'Reading the datafrom the excel
  For i = 1 To LastRow
      For j = 1 To Lastcol
          If j = Lastcol Then
              SLine = SLine &amp; Cells(i, j).Value
          Else
              SLine = SLine &amp; Cells(i, j).Value &amp; Deliminator
          End If
      Next j
      'Writing data in test file
      Print #FileNumber, SLine
      SLine = &quot;&quot;
  Next i
  Close #FileNumber
  MsgBox &quot;Text file has been generated&quot;
  
  End Sub
  </code></pre>
  <p>But the result of this reads from left to right instead of top to bottom in the notepad like <a href="https://i.stack.imgur.com/xmibe.png" rel="nofollow noreferrer">this</a>.
  I think the key is to detect if there is a keyword &quot;LAYER&quot; in a cell, if this is so, then it knows it should start printing to the next line (under the previous ones) in notepad.
  <a href="https://i.stack.imgur.com/ShEZT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ShEZT.png" alt="enter image description here" /></a></p>