Excel/Looping in macro


Dear Bob,

I have a workbook with over 50 work sheets; in which i need to delete the first 28 rows from all the sheets.

the below code is not working and returning an error:

Sub deleterows()
' deleterows Macro
   Dim ws As Worksheet

   ' Loop through all selected sheets.
   For Each ws In ActiveWorkbook.Worksheets
         Selection.Delete Shift:=xlUp
   Next ws

End Sub

I cannot put the finger on where i have a mistake. Pls help.

Also if i need to add an option for the macro to prompt the number of rows i need deleting within a workbook (not worksheet) is that possible?

Many thanks.


You didn't look thru all the sheets; you looked at the active sheet only and tried to delete rows 1:28 as many times as there are sheets!
Change this:

  For Each ws In ActiveWorkbook.Worksheets
         Selection.Delete Shift:=xlUp
   Next ws

to this:

  For Each ws In ActiveWorkbook.Worksheets
  Next ws
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Bob Umlas


I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/


Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Excellence, The Expert, Microsoft

BA in math, Hofstra University, 1965

Awards and Honors
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.