Code to return column # of matching string returning runtime error 91

mellymelle

New Member
Joined
Nov 25, 2016
Messages
45
This is part of a larger code to find column numbers of matching headings and deleting such columns.

Code:
Dim DateFind As Range, DateCol As Long
Set DateFind = Sheet1.Rows(1).Find(What:="Date", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
DateCol = DateFind.Column
Columns(DateCol).EntireColumn.Delete

The code is returning runtime error 91 object variable or with block variable not set...any ideas?
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Remove the .Column that you have on the end of your Set code line... or, perhaps, change it to Columns (note the "s" on the end) depending on what range you are actually trying to set.
 
Upvote 0
I'm guessing that the error is on this line:
Code:
Set DateFind = Sheet1.Rows(1).Find(What:="Date", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column

If yes, then its probably because Find method cannot find any cell with text "Date" in the row and returns a null object. So trying to read Column property of a null object causes this error.
 
Upvote 0
How about
Code:
Sheet1.Rows(1).Find(What:="Date", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).EntireColumn.Delete
You have declared DateFind as range, but were trying to give it a value
 
Last edited:
Upvote 0
You've dimensioned DateFind as a range object, but you are "setting" it to a column number which is a long. You have two choices:

1. Dimension DateFind as a range and remove the .Column from the end of your Find, or
2. Dimension DateFind as Long and remove the Set keyword from the Find line.
 
Upvote 0
Thank you all for your quick replies! Much appreciated all around.
JoeMo - thanks for the detailed explanation that helped me learn something for the day.
Fluff - your intuition is correct as all I'm looking to do is delete the columns with such headings much better to use the more straightforward code you provided.
 
Upvote 0
Glad We could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,788
Messages
6,126,907
Members
449,348
Latest member
Rdeane

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
Back
Top