Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Excel VBA; Delete Rows

Posted on 2011-02-23
3
Medium Priority
?
315 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:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

715 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