Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default weird loop problem- extra rows dissapearing

XL2003 on WinXP

I have a macro that opens a large (fixed width delimited .txt) data file,
parses it into cells, removes some extra header rows, then removes blank
lines (every other line). The problem is that I'm losing lots of extra rows
when the macro runs. I've double-checked this part of the code by comparing
a debug msgbox (below) to the results I get when I manually manipulate the
data- a difference of over 7000 extra rows seem to dissapear when the macro
runs in my source workbook.

'-------------------------------------------------------------------------------
'-----------------------
'delete the extra header rows
'-----------------------
Sheet1.Activate
Sheet1.Rows("1:6").Select
Selection.Delete Shift:=xlUp
Sheet1.Rows("2:4").Select
Selection.Delete Shift:=xlUp
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row

'-----------------------
'Delete all blank rows
'-----------------------
For i = LastRow To 1 Step -1
If Sheet1.Range("A" & Trim(Str(i))).Value = "" Then
Application.StatusBar = "Deleting blank row: " & Str(i)
Sheet1.Rows(i).Delete
End If
Next
LastRow2 = Cells(Cells.Rows.Count, "A").End(xlUp).Row

MsgBox "count #1 = " & LastRow & Chr(13) & Chr(13) & _
"count #2 = " & LastRow2
'-------------------------------------------------------------------------------

When run all together, the message box returns
count 1= 18234 (correct)
count 2= 1920 ** if only blank rows are deleted, this should be 9116! **

I visually confirmed that there are only 1920 rows of data after the macro
runs. When I import the raw data file manually, I get the 18000+ rows of
data, then I delete the 9 extra rows at the top and sort blank rows to the
bottom- I get 9116 rows of data, 7000+ more than my macro gave me.

I then pasted the two chunks of code above into a new workbook and ran them
separately on a fresh import of the raw data, and ended up with 9116...so
something weird is going on in the main workbook where I'm losing all the
extra rows.

Any ideas?

Many thanks,
Keith


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 152
Default weird loop problem- extra rows dissapearing

You said every other line (row) is blank. You can use:

On Error Resume Next 'In case there are no blank cells
Range("A:A").SpecialCells(xlCellTypeBlanks).Entire Row.Delete
On Error GoTo 0

which will delete all rows in which the cell in column A is blank.

Alan

"ker_01" wrote:

XL2003 on WinXP

I have a macro that opens a large (fixed width delimited .txt) data file,
parses it into cells, removes some extra header rows, then removes blank
lines (every other line). The problem is that I'm losing lots of extra rows
when the macro runs. I've double-checked this part of the code by comparing
a debug msgbox (below) to the results I get when I manually manipulate the
data- a difference of over 7000 extra rows seem to dissapear when the macro
runs in my source workbook.

'-------------------------------------------------------------------------------
'-----------------------
'delete the extra header rows
'-----------------------
Sheet1.Activate
Sheet1.Rows("1:6").Select
Selection.Delete Shift:=xlUp
Sheet1.Rows("2:4").Select
Selection.Delete Shift:=xlUp
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row

'-----------------------
'Delete all blank rows
'-----------------------
For i = LastRow To 1 Step -1
If Sheet1.Range("A" & Trim(Str(i))).Value = "" Then
Application.StatusBar = "Deleting blank row: " & Str(i)
Sheet1.Rows(i).Delete
End If
Next
LastRow2 = Cells(Cells.Rows.Count, "A").End(xlUp).Row

MsgBox "count #1 = " & LastRow & Chr(13) & Chr(13) & _
"count #2 = " & LastRow2
'-------------------------------------------------------------------------------

When run all together, the message box returns
count 1= 18234 (correct)
count 2= 1920 ** if only blank rows are deleted, this should be 9116! **

I visually confirmed that there are only 1920 rows of data after the macro
runs. When I import the raw data file manually, I get the 18000+ rows of
data, then I delete the 9 extra rows at the top and sort blank rows to the
bottom- I get 9116 rows of data, 7000+ more than my macro gave me.

I then pasted the two chunks of code above into a new workbook and ran them
separately on a fresh import of the raw data, and ended up with 9116...so
something weird is going on in the main workbook where I'm losing all the
extra rows.

Any ideas?

Many thanks,
Keith



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird Problem Gaurav[_2_] Excel Worksheet Functions 4 December 17th 07 04:27 PM
Weird problem Sue Excel Worksheet Functions 2 November 29th 05 12:10 PM
Weird Problem Patrick Excel Worksheet Functions 4 March 28th 05 02:29 AM
Weird problem! Mark Thorpe Excel Programming 0 June 24th 04 08:40 PM
Weird VBA problem Bob[_55_] Excel Programming 3 May 12th 04 11:39 PM


All times are GMT +1. The time now is 05:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"