Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Simple For-Loop gives 1004 error using variable to EntireRow.Delete

Sorry I'm a real newbie to Excel and VB but here goes:
Why does this code give me an error in Excel?

Sub Eds1()
Dim i As Integer
For i = 0 To 200 Step 2
Sheet1.Rows(i).EntireRow.Delete
Next i
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default Simple For-Loop gives 1004 error using variable to EntireRow.Delet

Hi,

this is because the rows in Excel start at 1, but yout loop (that references
the rows) starts at 0

so try this:
For i = 1 To 200 Step 2
Sheet1.Rows(i).EntireRow.Delete
Next i

HTH

Philip


"EdMX" wrote:

Sorry I'm a real newbie to Excel and VB but here goes:
Why does this code give me an error in Excel?

Sub Eds1()
Dim i As Integer
For i = 0 To 200 Step 2
Sheet1.Rows(i).EntireRow.Delete
Next i
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Simple For-Loop gives 1004 error using variable to EntireRow.Delete

In the first loop it tries to delete row zero. There is no row zero.
The first row is 1. Perhaps this change will achieve your goal:

For i = 1 to 200 Step 2

HTH,
Steve Hieb
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Simple For-Loop gives 1004 error using variable to EntireRow.Delete

And after you look at your results, you may want to start at the bottom and work
up:

Sub Eds2()
Dim i As Long
For i = 200 To 1 Step -2
Sheet1.Rows(i).EntireRow.Delete
Next i
End Sub

EdMX wrote:

Sorry I'm a real newbie to Excel and VB but here goes:
Why does this code give me an error in Excel?

Sub Eds1()
Dim i As Integer
For i = 0 To 200 Step 2
Sheet1.Rows(i).EntireRow.Delete
Next i
End Sub


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run time error 1004 in loop danpt Excel Discussion (Misc queries) 4 February 11th 10 04:02 PM
run time error 1004 in loop danpt Excel Discussion (Misc queries) 1 February 10th 10 11:45 PM
runtime error '1004' delete Method of Range Class Failed Tom Kennedy Excel Programming 0 April 14th 04 08:08 PM
runtime error '1004' delete Method of Range Class Failed Tom Ogilvy Excel Programming 0 April 1st 04 04:09 AM
Simple problem refering to variable in For ... Next loop ... Chris Excel Programming 1 July 15th 03 01:00 AM


All times are GMT +1. The time now is 11:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"