Solved

MS Excel VBA; Delete Rows

Posted on 2011-02-23
3
295 Views
Last Modified: 2012-05-11
Expert's,
What Excel 2007 VBA code would do the following:
Delete Rows 1 through 100 on sheets 1, 2, 3 at the same time while not deleting anything in sheet4?  I am not aiming to just clear contents.  I want it to delete the entire rows.
0
Comment
Question by:armyssg
3 Comments
 
LVL 24

Accepted Solution

by:
broomee9 earned 250 total points
ID: 34961824
Try this:
Sub DeleteRows()

    Dim i As Integer
    
    For i = 1 To 3
        Sheets("Sheet" & i).Activate
        Rows("1:100").Delete Shift:=xlUp
    Next i
    
End Sub

Open in new window

Book1.xlsm
0
 
LVL 18

Assisted Solution

by:Cory Vandenberg
Cory Vandenberg earned 250 total points
ID: 34964316
I just want to point out that you don't need to Activate the sheet in order to work with it.  All that does is make the ActiveSheet object the implicit owner of the Rows collection.

Also, althought I think I have never advocated using the Selection object unless you actually want to see the Selection change on screen, there is a way to use .Select to avoid looping in VBA, at least as far as your code is concerned.  (It may be looping in the background for all I know.)  This is illustrated in the second example, and is similar to what you would get using the Macro Recorder.   You could use the worksheet names instead of indexes in the Array().

If you use broomee's solution or my second option and don't want to see the screen change, just wrap the code with the following two lines

Sub ....
Application.ScreenUpdating = False
<code>
Application.ScreenUpdating = True
End Sub

WC
Sub DeleteRows()
  Dim ws As Worksheet
  Dim sh as Long

  For sh = 1 to 3
    Set ws = Worksheets(i)
    ws.Rows("1:100").Delete Shift:=xlUp
  Next sh
End Sub

Sub DeleteRows()
    Sheets(Array(1, 2, 3)).Select
    Rows("1:100").Select
    Selection.Delete Shift:=xlUp
End Sub

Open in new window

0
 

Author Closing Comment

by:armyssg
ID: 34966511
Both were excellent solutions as I tried them both.  They both accomplished exactly what I needed very quickly.  Thank you both for the quick replies/solutions.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now