VBA Loop troubles

hfler

Board Regular
Joined
Jun 10, 2008
Messages
95
HTML:
Public Function bidsearch(monthyear As String, k As Double) As Double
Dim searchrange As Range
Dim dummy As Range
Set searchrange = Range("bbg_strike", Range("bbg_strike").End(xlDown))
For Each dummy In searchrange
    If dummy.Value = k And dummy.Offset(-1, 0) = monthyear Then
    bidsearch = dummy.Value.Offset(2)
    Else
    bidsearch = "error"
    End If
Next dummy
 
End Function

Does anyone have any idea why this return "#VALUE!"?


Thanks!
 
My mistke. I should include that I'm a new VBA user - I mixed up by column and row offset commands. In response to your questions, I have manthyear, and I know k and monthyear will match, because I entered in some test values to make sure they do.

Should look like this,

Code:
    If dummy.Value = k And dummy.Offset(0, -1).Value = monthyear Then
    bidsearch = dummy.Offset(0, 4).Value
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
No, still getting that VALUE! returned. Here's my new code in full now,

Code:
Public Function bidsearch(monthyear As Double, k As Double) As Double
Dim searchrange As Range
Dim dummy As Range
Set searchrange = Range(Range("bbg_strike"), Range("bbg_strike").End(xlDown))
For Each dummy In searchrange
    If dummy.Value = k And dummy.Offset(0, -1).Value = monthyear Then
    bidsearch = dummy.Offset(0, 4).Value
    Else
    bidsearch = "error"
    End If
Next dummy
        
End Function
 
Upvote 0
Try this:

Code:
Public Function bidsearch(monthyear As Double, k As Double) As Variant
Dim searchrange As Range
Dim dummy As Range
Set searchrange = Range("bbg_strike")
For Each dummy In searchrange
    If dummy.Value = k And dummy.Offset(0, -1).Value = monthyear Then
    bidsearch = dummy.Offset(0, 4).Value
    Else
    bidsearch = "error"
    End If
Next dummy
        
End Function

Part of the problem is that you have declared your Function as a double. You can't have "error" as a response as that is a string. Changed the Function to a Variant.
 
Upvote 0
Ah. That worked to return "error". But there should still be an actual numerical value returned. I think I need to add on more parameter to check in my if statement, because there might be duplicate entries that meet the parameters, as of now. Thanks again.
 
Upvote 0

Forum statistics

Threads
1,225,156
Messages
6,183,239
Members
453,152
Latest member
ChrisMd

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