Copying cell values to a external file in VBA
Hi, Matt-
I saw where you were going with the code, and rather than revise your
code I thought I'd show an alernate way to write it- this is a touch
more compact. Please note if you copy and paste this into your file:
this newsgroup interface word-wraps, so longer lines of code that need
to be on one line for the compiler may show up on multiple lines, and
you'll need to remove line breaks.
I wrote this code to match your description of the data's layout- for
instance, the code should stop when it encounters a value in column A
that is not a 2. What happens, though, if there is a blank line or
another value that appears with more "2" rows below? Also, if you run
this report on a daily basis you can modify the output filename via
code to include a date and or timestamp. (Sorry if you knew that
already.)
Let us know what you think~
Dave O
Sub Report_Body()
Dim Output As String
Range("a1").Select 'this assumes your data starts in cell A1
Open "C:\Documents and Settings\mcragg\My Documents\Excel Reports\CWPS
Folder\Record.txt" For Output As #1
Do While InStr(1, ActiveCell.Value, "2") 0 'run when the entry in col
A contains a 2
Output = ActiveCell.Value & "," & ActiveCell.Offset(0, 1).Value & ","
& ActiveCell.Offset(0, 2).Value & "," & ActiveCell.Offset(0, 3).Value &
"," & ActiveCell.Offset(0, 4).Value & "," & ActiveCell.Offset(0,
5).Value
Print #1, Output
ActiveCell.Offset(1, 0).Select
Loop
Close #1
Range("a1").Select
Msgbox "Done."
End Sub
|