always_confused
Board Regular
- Joined
- Feb 19, 2021
- Messages
- 68
- Office Version
- 2016
- Platform
- Windows
Hello, I have a sheet with 35 columns. I would like to create a For loop to go through all the headers of these columns, find a specific one and return the column underneath it as a Range. I have tried
But it doesn't seem to want to do Range = Column. I get the error "Object variable or With block variable not set". Is it even possible to set a range to a column? Or is there another way to set up the For loop? My main issue is that ranges are named with letters as far as I understand, but the for loop variable is an integer so I'm trying to use column but maybe there's a better way to do this? I really appreciate any help, thanks
VBA Code:
Dim rng As Rang
With Sheets("MySheet"). UsedRange
For i = 1 To .Columns.count
If Cells(1,i).value = "column_name" Then
rng = Column(i)
Next i
End With
But it doesn't seem to want to do Range = Column. I get the error "Object variable or With block variable not set". Is it even possible to set a range to a column? Or is there another way to set up the For loop? My main issue is that ranges are named with letters as far as I understand, but the for loop variable is an integer so I'm trying to use column but maybe there's a better way to do this? I really appreciate any help, thanks