Solved

MS Excel VBA; Delete Rows

Posted on 2011-02-23
3
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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.
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 on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

628 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