Ranges - VBA - Why does this code not work?

timbram

I am try to fill some gaps in my VBA knowledege and I am confused as to why this code does not work in VBA:

Sub RangeTest()

Dim TemplateRange As Range
Set TemplateRange = ThisWorkbook.Worksheets("Template").Range("A7:BO200")

For Each thing In ThisWorkbook.Worksheets("Template").Range("A7:BO200").Columns(2)
    Debug.Print thing
Next thing

End Sub

Is a range not a group of other objects that you can iterate through? Thinking about it now, I guess it might not be. I noticed that I can iterate through the .values of the range. But I guess the range itself is simply one object?

Barranka

You must say which collection in the .Columns() object you want to use; in this case, the .Cells collection seems what you want:

Sub RangeTest()
    Dim TemplateRange As Range
    Set TemplateRange = ThisWorkbook.Worksheets("Template").Range("A7:BO200")
    For Each thing In TemplateRange.Columns(2).Cells
    ' This is what you want to use, I think --^^^^^^
        Debug.Print thing
    Next thing
End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related