Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Object Variable Not Set

I am getting and error as shown ' Object Variable Not Set' But I have a
valid range as I can see its properties in the watch window, 'key' is the
value I am searching for and this is alos valid. I know I have the right
range as It has the right number of cells in the cell count.

Any Ideas ?

Public Function lookupRange(ByRef rangeName As Range, key As Variant, col As
Presets) As Variant

Dim rowIndex As Integer
Dim myStr As String
Dim OpenBrktPos As Long
Dim CloseBrktPos As Long
Dim startRange As Range

Set startRange = rangeName.Cells(1, 1)

' ***** LINE BELOW CAUSES THE ERROR *********
myStr = rangeName.Find(what:=key,
SearchOrder:=xlByColumns).Address(False, False, xlR1C1, False, startRange)

OpenBrktPos = InStr(1, myStr, "[", vbTextCompare)
CloseBrktPos = InStr(1, myStr, "]", vbTextCompare)
rowIndex = CInt(Mid(Left(myStr, CloseBrktPos - 1), OpenBrktPos + 1)) + 1

lookupRange = Range(rangeName).Cells(rowIndex, col)


End Function


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Object Variable Not Set

I know what the error was caused by now, it threw the error because it could
not find an object associated with the key.

Not sure how best to handle that , any ideas ?




"Goofy" wrote in message
...
I am getting and error as shown ' Object Variable Not Set' But I have a
valid range as I can see its properties in the watch window, 'key' is the
value I am searching for and this is alos valid. I know I have the right
range as It has the right number of cells in the cell count.

Any Ideas ?

Public Function lookupRange(ByRef rangeName As Range, key As Variant, col
As Presets) As Variant

Dim rowIndex As Integer
Dim myStr As String
Dim OpenBrktPos As Long
Dim CloseBrktPos As Long
Dim startRange As Range

Set startRange = rangeName.Cells(1, 1)

' ***** LINE BELOW CAUSES THE ERROR *********
myStr = rangeName.Find(what:=key,
SearchOrder:=xlByColumns).Address(False, False, xlR1C1, False, startRange)

OpenBrktPos = InStr(1, myStr, "[", vbTextCompare)
CloseBrktPos = InStr(1, myStr, "]", vbTextCompare)
rowIndex = CInt(Mid(Left(myStr, CloseBrktPos - 1), OpenBrktPos + 1)) +
1

lookupRange = Range(rangeName).Cells(rowIndex, col)


End Function



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Object Variable Not Set

Untested:

rangeName is already a range. So don't wrap it in range(rangename).

lookupRange = Range(rangeName).Cells(rowIndex, col)
becomes
lookupRange = rangeName.Cells(rowIndex, col)



Goofy wrote:

I am getting and error as shown ' Object Variable Not Set' But I have a
valid range as I can see its properties in the watch window, 'key' is the
value I am searching for and this is alos valid. I know I have the right
range as It has the right number of cells in the cell count.

Any Ideas ?

Public Function lookupRange(ByRef rangeName As Range, key As Variant, col As
Presets) As Variant

Dim rowIndex As Integer
Dim myStr As String
Dim OpenBrktPos As Long
Dim CloseBrktPos As Long
Dim startRange As Range

Set startRange = rangeName.Cells(1, 1)

' ***** LINE BELOW CAUSES THE ERROR *********
myStr = rangeName.Find(what:=key,
SearchOrder:=xlByColumns).Address(False, False, xlR1C1, False, startRange)

OpenBrktPos = InStr(1, myStr, "[", vbTextCompare)
CloseBrktPos = InStr(1, myStr, "]", vbTextCompare)
rowIndex = CInt(Mid(Left(myStr, CloseBrktPos - 1), OpenBrktPos + 1)) + 1

lookupRange = Range(rangeName).Cells(rowIndex, col)

End Function


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Object Variable Not Set

Thanks Dave

I'd already gotten to this one, but thanks for replying


"Dave Peterson" wrote in message
...
Untested:

rangeName is already a range. So don't wrap it in range(rangename).

lookupRange = Range(rangeName).Cells(rowIndex, col)
becomes
lookupRange = rangeName.Cells(rowIndex, col)



Goofy wrote:

I am getting and error as shown ' Object Variable Not Set' But I have a
valid range as I can see its properties in the watch window, 'key' is the
value I am searching for and this is alos valid. I know I have the right
range as It has the right number of cells in the cell count.

Any Ideas ?

Public Function lookupRange(ByRef rangeName As Range, key As Variant, col
As
Presets) As Variant

Dim rowIndex As Integer
Dim myStr As String
Dim OpenBrktPos As Long
Dim CloseBrktPos As Long
Dim startRange As Range

Set startRange = rangeName.Cells(1, 1)

' ***** LINE BELOW CAUSES THE ERROR *********
myStr = rangeName.Find(what:=key,
SearchOrder:=xlByColumns).Address(False, False, xlR1C1, False,
startRange)

OpenBrktPos = InStr(1, myStr, "[", vbTextCompare)
CloseBrktPos = InStr(1, myStr, "]", vbTextCompare)
rowIndex = CInt(Mid(Left(myStr, CloseBrktPos - 1), OpenBrktPos + 1))
+ 1

lookupRange = Range(rangeName).Cells(rowIndex, col)

End Function


--

Dave Peterson



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Variable Not Set Error on Selection object Jean Excel Worksheet Functions 3 July 24th 06 06:45 PM
Getting inconsistent Error 91-Object variable or With block variable not set mfq Excel Programming 0 December 14th 05 06:08 PM
Run-time Error'91: Object variable or With block variable not set DynamiteSkippy Excel Programming 4 September 26th 05 07:47 AM
Run-time error '91': "Object variable or With block variable not set Mike[_92_] Excel Programming 2 December 30th 04 10:59 AM
Cells.Find error Object variable or With block variable not set Peter[_21_] Excel Programming 2 May 8th 04 02:15 PM


All times are GMT +1. The time now is 06:18 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"