I have run into a strange behavior when looping through a range in Excel VBA. This code was previously working, and suddenly has begun to give me errors today. I have list objects/columns that I am referencing and looping through those ranges.
Option Explicit
sub loopThroughARange()
Dim r as range
Debug.Print VarType(r) 'returns 9 - 'r' is an object
For Each r In wb_in.Sheets(optionName & " Option").ListObjects("tableName").ListColumns("tableColumn").DataBodyRange
Debug.Print r.Address 'prints out the address
Debug.Print VarType(r) 'returns 8 - 'r' is now a string?!?
functionWithRangeInput(r) 'throws an error
Next r
End sub
Private Function functionWithRangeInput(r as Range)
Debug.Print r.Address 'doesn't even get here
End Function
I don't see any issue with this code and I do not understand why my 'for' loop would suddenly convert 'r' to a string type, even though it is declared as a range and is being looped as a range.
Does anybody have any suggestions about how this transformation is occurring in the first place and how to prevent it?