Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Excel VBA; Delete Rows

Posted on 2011-02-23
3
Medium Priority
?
323 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:
Tracy earned 1000 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 1000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

782 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