Solved

MS Excel VBA; Delete Rows

Posted on 2011-02-23
3
276 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
Comment Utility
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:WarCrimes
WarCrimes earned 250 total points
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
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.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

763 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

8 Experts available now in Live!

Get 1:1 Help Now