always_confused

Board Regular
Joined
Feb 19, 2021
Messages
66
Office Version
  1. 2016
Platform
  1. 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

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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
6,190
Office Version
  1. 365
Platform
  1. Windows
try changing this line of code

VBA Code:
rng = Column(i)
to

VBA Code:
set rng = Column(i)
 

Watch MrExcel Video

Forum statistics

Threads
1,129,774
Messages
5,638,268
Members
417,019
Latest member
PKDP

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top