title: Macro to iterate through all rows in all columns of a worksheet and compare a cell to copy the row to a text file
body: <p>I am writing a VB excel macro which iterates through all the worksheets in a opened workbook.
  In each worksheet, there are few "Y" stored in column K. I want to compare the values in K column to "Y" and if it is equal, then the columns A,B,D,H of the same row should get insert to a text file in the format tab delimited. </p>
  
  <p>This is the code which i have tried. In this code i am inserting only column K to the text file. But I also want to insert the values of A,B,D,H and K column values to the text fil separated by a tab.</p>
  
  <p>Please help me out.</p>
  
  <p>Code which I tried is</p>
  
  <pre><code>  Sub Button3_Click()
    Dim fso, myfile, I As Integer, mycount As String, x As String
    Dim curCell As Range
    Dim sh As Worksheet 
    x = "Y"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set myfile = fso.CreateTextFile("d:\RECP_IMP_COLUMNS.txt", True)
    myfile.WriteLine ("Work Sheet Names are as follows")
  
  
      For Each sh In ActiveWorkbook.Worksheets
  
        For Each curCell In Sheet4.Range("K1:K300").Cells
         If (curCell.Value = x) Then
          myfile.WriteLine (curCell)
         End If
        Next curCell
  
      Next
  
      myfile.Close
    End Sub
  </code></pre>