Copying then deleting every second row
Joe,
I'd use VBA. Presuming your file is 'plain text' and comma delimited the
code below would work. It gets more complicated if you have a more structured
document (comma delimited with quotes marking text etc). You need to set a
reference to 'Microsoft Scripting Runtime' to use FileSystemObject/TextStream.
Public Sub LoadFile()
Dim sFileName As String
Dim fso As New FileSystemObject
Dim fsoTextStream As TextStream
Dim sOddLine As String
Dim sEvenLine As String
Dim arrOddLine() As String
Dim arrEvenLine() As String
Dim lRowPointer As Long
Dim iColPointer As Integer
sFileName = "C:\myfile.txt" 'put you file name here
Set fsoTextStream = fso.OpenTextFile(sFileName, ForReading)
lRowPointer = 1
ThisWorkbook.Sheets(1).Cells.ClearContents
Do Until fsoTextStream.AtEndOfStream
sOddLine = fsoTextStream.ReadLine
sEvenLine = fsoTextStream.ReadLine
arrOddLine() = Split(sOddLine, ",")
arrEvenLine() = Split(sEvenLine, ",")
For iColPointer = 1 To 7
ThisWorkbook.Sheets(1).Cells(lRowPointer, iColPointer) =
arrOddLine(iColPointer - 1)
Next iColPointer
For iColPointer = 8 To 9
ThisWorkbook.Sheets(1).Cells(lRowPointer, iColPointer) =
arrEvenLine(iColPointer - 8)
Next iColPointer
lRowPointer = lRowPointer + 1
Loop
fsoTextStream.Close
End Sub
Regards,
Chris.
--
Chris Marlow
MCSD.NET, Microsoft Office XP Master
"Joseph" wrote:
I have a report that comes from our mainframe. Each record is on two
rows. I need to copy every second row (i.e. A2:B2) to the right of the
data on the first row (i.e. H1) and then delete the second row.
Here's what I tried:
1) Filter for something unique in the first row of each record
2) Select visible cells in column H
3) Add a formula to column H to pick up the values from the second row
4) Convert formulas to values in column H
5) Filter for something unique in the second row of each record
6) Select visible cells
7) Delete (visible) rows (i.e. every seond row)
This worked fine when I was working with 10,000 records. But when my
report contains more than 20,000 records, Excel cannot select visible
cells. I get a message saying it's too complex.
I tried looping through the cells and it gets the job done but with
40,000 records, it's s-l-o-o-o-o-w.
Any suggestions for another workaround. Assume that the first row in
each record has data in columns A:G and the second row has data in
columns A:B.
I appreciate your help.
Joe
|