If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Problem setting range object

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-19-04, 14:29
robojam robojam is offline
Registered User
 
Join Date: Feb 2004
Location: Charlotte, NC
Posts: 79
Question Problem setting range object

Hi

I have a problem setting a range object to be returned from one of my functions. I use a set keyword to set the return value (which is accepted), but I get the error:

"Object variable or With block variable not set (Error 91)"

when I run the code.

I call the function with:

Code:
rTarget = FindVal(wstTarget.Name, sDate, sTOB)
and the function itself is:

Code:
Function FindVal(sSheet As String, sDate As String, sTOB As String) As Range

    ' Return cell range for target value
    Dim iRow As Integer
    Dim iCol As Integer
    
    iRow = 2
    iCol = 2
    
    Dim wstSheet As Worksheet
    Set wstSheet = ThisWorkbook.Worksheets(sSheet)
    
    Do Until wstSheet.Cells(iRow, 1).Value = sTOB
        iRow = iRow + 1
    Loop
    
    Do Until wstSheet.Cells(1, iCol).Value = sDate
        iCol = iCol + 1
        
        If wstSheet.Cells(1, iCol).Value = "" Then
            Exit Do
        End If
    Loop
    
    Set FindVal = wstSheet.Cells(iRow, iCol)

End Function
Does anyone have any idea what is wrong? When I debug the code, I get the error on the End Function line, but it steps through all the rest of the code in the fuction OK.

Thanks
__________________
Make something idiot proof and someone will make a better idiot...
Reply With Quote
  #2 (permalink)  
Old 08-19-04, 21:11
savbill savbill is offline
Registered User
 
Join Date: Feb 2004
Posts: 533
Quote:
Originally Posted by robojam
Set FindVal = wstSheet.Cells(iRow, iCol)
End Function

Does anyone have any idea what is wrong?
I have never seen the 'Set' statement used in returning a function value, at least not like this. Doesn't mean its not possible or correct, Didn't test it.

You might try first setting an object variable then assigning the Var to your function return value.

Set nVal = wstSheet.Cells(iRow, iCol)
FindVal = nVal
__________________
~

Bill
Reply With Quote
  #3 (permalink)  
Old 08-20-04, 09:08
MikeTheBike MikeTheBike is offline
Registered User
 
Join Date: Apr 2004
Location: Derbyshire, UK
Posts: 714
Hi

I thing this is the problem, you need to also 'Set' the rTarget also

Set rTarget = FindVal(wstTarget.Name, sDate, sTOB)

The Set Keyword assigns an object reference to a variable, therefore to change the assigment of any object (rTarget is an object variable and FindVal returns an object) you need the Set ketword.

HTH

MTB
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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