I'm iterating through column "A" of each row in a range determined by .UsedRange(), and writing the row number to the cell. The strange behaviour I'm seeing is when the row number is written to the cell, the consecutive row number is written only to every second cell:
(1,1) = "1"
(2,1) = empty
(3,1) = "2"
(4,1) = empty
(5,1) = "3"
(6,1) = empty
(7,1) = "4"
...
The worksheet has 10 rows, with empty cells in column "A", and single words in every cell in both column "B" and "C". When I step through the code I can see the row count (oRow.Row) iterate consecutively as I'd expect, but the values are written elsewhere. I tried resetting .UsedRange() as mentioned here: http://www.j-walk.com/ss/excel/tips/tip73.htm but there was no change with the outcome.
The code I'm using is:
Option Explicit
Sub IterateAndWrite()
Dim oRange As Range
Dim oRow As Range
Dim lCount As Long
Set oRange = ThisWorkbook.Worksheets(1).UsedRange
For Each oRow In oRange.Rows
oRow.Cells(oRow.Row, 1).Value = oRow.Row
' Reset the range
'lCount = ActiveWorkbook.Worksheets(1).UsedRange.Rows.Count
Next oRow
End Sub
Where am I going wrong?
.Cellsmethod), and a quick revision to your code :) - David Zemens