ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Simple For-Loop gives 1004 error using variable to EntireRow.Delete (https://www.excelbanter.com/excel-programming/318092-simple-loop-gives-1004-error-using-variable-entirerow-delete.html)

EdMX

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

Philip

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


Steve Hieb

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

Dave Peterson[_5_]

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


All times are GMT +1. The time now is 01:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com