1

I have a table with 22 rows where I want to go through column B only. My loop goes beyond the 22 rows and reaches 259 for some reason.

Code

Debug.Print "Starting..."
Dim tempRange As Range
Set tempRange = Sheet18.UsedRange.Columns("B")

Dim i As Integer
i = 0
For Each Cell In tempRange.Cells
    i = i + 1
Next Cell
Debug.Print "Count: " & i

Intermediate Window

Starting...
Count: 259

Screenshot showing Sheet18 with 22 rows

Table with 22 rows

Screenshot showing table reference with 22 rows

Table reference showing 22 rows

BigBen
  • 46,229
  • 7
  • 24
  • 40
TechFanDan
  • 3,329
  • 6
  • 46
  • 89

1 Answers1

7

UsedRange has issues, as noted here.

Since you have a table, or ListObject, just iterate through the cells in the ListColumn.

Dim myTbl as ListObject
Set myTbl = Sheet18.ListObjects("Table24")

Dim rng as Range
For Each rng in myTbl.ListColumns(2).DataBodyRange
    ...
Next rng
BigBen
  • 46,229
  • 7
  • 24
  • 40