txt file from filtered data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a worksheet that has 6 columns. The number of rows can vary. I need
to filter the worksheet on 3 of the columns to narrow the data down to what I
need to extract. Then I need to create a text file using the filtered data
so that cells A1, B1, & C1 are on a line and then D1, E1, & F1 are on the
next line. I need to repeat this in the text file for each row in the
filtered worksheet. Is this possible?
 
Do you need to actually filter the sheet (and leave it filtered) or is the
filtering just to find the data to extract? You don't really need to filter
since you can have your code check the conditions you need. Without knowing
more, I can only outline a procedure:

Sub ExtractData()

Dim DataRange as Range, ThisRow as Integer, OutputStr as String

Open "C:\EXAMPLE\Mytextfile.txt" For Append as #1 ' or, For Output

Set DataRange=Sheets("DataSheetName").Range("DataRange")
For ThisRow = 1 to DataRange.Rows.Count
' Now read the data, line by line, using the Offset method to find the cell
you want to check, e.g:
If DataRange.Offset(ThisRow-1,2) = ??? _ ' This checks the 3rd column in
your list
Then OutputStr=DataRange.Offset(ThisRow-1,3).Range("A1").Value & "," _
& DataRange.Offset(ThisRow-1,4).Range("A1").Value
' The above creates a comma-delimited entry based on the values in columns 4
& 5
' (Note that .Offset begins with 0 for the current row/column)

Write #1, OutputStr
Next ThisRow

Close #1
End Sub
 
I really don't know how or where to begin. Fixed columns would probably work
but I could just separate each cell with a few spaces on each line of the
text file. If I could do some type of autofilter or input box to ask for the
3 criterias to narrow it down to only the data I needed would be good leaving
only the filtered text and deleting everything else. I can use the
autofilter built into Excel if I need to. the main thing is to figure out
how to dump this into the txt file in the format I need with the certain
cells on one line, other cells on the next line, and then insert a blank line
and do it all over again for each remaining row. Thanks.
 
I guess I don't need to filter it the way I was thinking as long as I can
extract the rows that meet my criteria (status, date, employee). Then I only
need to pull from each of those rows specific cells for the txt file.
 
Back
Top