Out Of range Error with Find

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
I've used similar code in the past, but can't figure out what I'm missing. Troubleshooting I've reduced the find down to a single string and still get "Out of Range." Help is appreciated!
Some of the comments are notes to myself as to why code may be different than code I've used in the past.

VBA Code:
Sub MakeRanges_m()
   '
    Sheets("OHR Report").Activate
    '
    Dim LastCol             As Integer
    Dim LastRow             As Long
    Dim rng_HeaderRow       As Range
    Dim ThisWb              As Workbook
    Dim ThisWs              As Worksheet
    '
    Set ThisWb = ActiveWorkbook
    Set ThisWs = ActiveSheet
    '
    With ThisWb
    With ThisWs
    '
    LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
'    Range("A1", Cells(LastCol)).Name = "rng_HeaderRow" 'I don't understand why this doesn't work with a "DIM rng_HeaderRow As Long"  like "LastRow", but changed it to a named range
'    Range("A1", Cells(LastRow, LastCol)).Name = "rng_tbl"
'---Start create ranges---
    .Names.Add Name:="rng_HeaderRow", RefersTo:=Range("A1", Cells(LastCol))
'  Range("A1", Cells(LastCol)).Name = "rng_HeaderRow" wouldn't work, no idea why . . . 
'
    Set Rng = Range("rng_HeaderRow")
    With Rng
            .Find(What:="Primary TW Zip Code", LookAt:=xlAll, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "c_p_zipcode"
    End With
   Cells(1, 1).Select
   Cells(1, 1).Activate
    End With 'ThisWs
    End With 'ThisWb
End Sub
TIA

Ron
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
A copy of the range (it looks like a Header Row) would be helpful. Can you create a mini-sheet and share it?
 
Upvote 0
This does not appear to be a valid range reference:
VBA Code:
RefersTo:=Range("A1", Cells(LastCol))
Try this:
VBA Code:
RefersTo:=Range(Cells(1, "A"), Cells(LastRow, LastCol))
 
Upvote 0
This does not appear to be a valid range reference:
VBA Code:
RefersTo:=Range("A1", Cells(LastCol))
Try this:
VBA Code:
[QUOTE="Bosquedeguate, post: 6098760, member: 508385"]
A copy of the range (it looks like a Header Row) would be helpful.   Can you create a mini-sheet and share it?
[/QUOTE]
Thanks, will have to do it from home. Sharing sites are all blocked from work. "But" you prompted me to look deeper at the named range and it's not what I expected!
It's ='OHR Report'!$A$1:$S$13216
And it should be 'OHR Report'!$A$1:$S$1

So there is the "real" problem. Now to figure out how I got there . . .
 
Upvote 0
This does not appear to be a valid range reference:
VBA Code:
RefersTo:=Range("A1", Cells(LastCol))
Try this:
VBA Code:
RefersTo:=Range(Cells(1, "A"), Cells(LastRow, LastCol))
Same result. I may have looked at the wrong range name, :rolleyes:


1692989577642.png


What I find crazy is only the zip code ranges are not created. I inserted the ".Range("A1").Activate" trying to be sure to set the arguments explicitly. Am I doing this correctly? I also tried a complete new With Range and get the same errors. it's after the first zip code is found that the find "Primary TW Zip Code" fails. Same if I rem it and try "Secondary TW Zip Code" with "LookAt:=xlAll"

VBA Code:
With Range("rng_HeaderRow")
            .Find(What:="UserName", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "c_UserName"
            .Range("A1").Activate
            .Find(What:="Employee ID", LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "c_EmployeeID"
            .Range("A1").Activate
            .Find(What:="Zip Code", LookAt:=xlAll, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "c_zipcode"
            .Range("A1").Activate
            .Find(What:="Primary TW Zip Code", LookAt:=xlAll, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "c_p_zipcode"
            .Range("A1").Activate
            .Find(What:="Secondary TW Zip Code", LookAt:=xlAll, SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "c_s_zipcode"
    End With 'Range("rng_HeaderRow")
 
Upvote 0
Sorry, if you just want the first row, try this instead:
VBA Code:
RefersTo:=Range(Cells(1, "A"), Cells(1, LastCol))
 
Upvote 0
LookAt:=xlAll should be LookAt:=xlWhole
and you don't need
.Range("A1").Activate
 
Upvote 1
Solution
I apologize for my belated response. Thank all both very much.I
Bosquedeguate prompted me to look at the range that had an error.
Akuini spotted my bad enumeration and prompted me to re-look at the Find options.
Joe4 reminded me that I'd re-used less than best practice code. I thought I'd purged my LastRow of the old code. Bad "assumption."

All combined together fixed my code!

I had placed the
VBA Code:
.Range("A1").Activate
in my code thinking maybe the search wasn't returning to the top left of the range. I found the answer that unless the start point is set, then the search starts at the top-left cell of the range.

Again, thank you all very much!

Ron
 
Upvote 0
then the search starts at the top-left cell of the range.

Just to be clear the Find starts after the top-left cell of the range. If you want the entire range searched then you start the Find after the last cell in the range.
Rich (BB code):
With Range("rng_HeaderRow")
            .Find(What:="UserName", LookAt:=xlPart, After:=.Cells(.Cells.Count), SearchOrder:=xlByRows, SearchDirection:=xlNext).Name = "c_UserName"
'            Rest of code
End With
 
Upvote 0
the Find starts after the top-left cell of the range.
That's what I thought and then obviously misread the Microsoft VBA Library when I rechecked the option after reading Akuini's response. <slaps forehead>
"Find (What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)"
And the previous paragraph where, as you point out, it states "the specified cell isn't searched until the method wraps back around to this cell." That's the reasoning I had for forcing the Find to re-start from "A1" when the best option would be to simply tell Find where to start using your example which I would not have thought of.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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