Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Is there an easy way to delete rows in Excel?


Reply
 
Thread Tools Display Modes
  #1  
Old 04-29-2002, 02:28 PM
jcarlson
Guest
 
Posts: n/a
Default Is there an easy way to delete rows in Excel?


Here is the problem.

Every once in a while, I will have over 150 rows to delete. I have tried looping through all 150 rows, and deleting each one seperately, but the delete seems to slow down after a while.

For example

For i = 1 to 150
rows(i).delete
Next i


I have also tried to select the rows, and delete the selection, but I can't get my array to join correctly, I get an invalid range error. For example

Dim arrDelete() as string

For i = 1 to 150
Redim Preserve arrDelete(i)
arrDelete(i) = i & ":" & i
Next

ActiveSheet.Range(join(arrDelete, ",")).Delete

Does anyone have a better idea, or know why the delete of a single row would slow down?

Thanks
-Jeff
Reply With Quote
  #2  
Old 04-29-2002, 05:20 PM
Timbo's Avatar
Timbo Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

Here's something I stick into most of my projects - should be in a class really
Code:
Sub UsedRange_DeleteRows(strSheet$, intRow1%)
Dim intRow2%

On Error Resume Next

    With Sheets(strSheet$)
        intRow2% = .Cells.Find("*", .Range("A1"), , , , xlPrevious).Row
        
        If intRow2% >= intRow1% Then _
            .Rows(intRow1% & ":" & intRow2%).EntireRow.Delete
            
    End With

End Sub
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
  #3  
Old 04-29-2002, 05:21 PM
jcarlson
Guest
 
Posts: n/a
Default

Thanks Timbo. I'll give it a try.

Have you experienced any similar problems with performance when deleting rows?

-Jeff
Reply With Quote
  #4  
Old 04-29-2002, 05:41 PM
Timbo's Avatar
Timbo Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

not really, especially when 'ScreenUpdating' is set to False before the action...
As you already suspected, deleting the entire range rather than 1 row at a time is way quicker!
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
  #5  
Old 05-01-2002, 05:55 PM
Brahmin
Guest
 
Posts: n/a
Default

The following works. The example isn't the most brilliant but it certainly didn't take long to complete.


Sub x()
Delrows 2, 149
End Sub


Private Sub Delrows(i As Integer, j As Integer)
ActiveSheet.Range("A" & Trim(i) & ":A" & Trim )).EntireRow.Delete
End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->