Type mismatch using Iif

ronga2001

New Member
Joined
May 29, 2019
Messages
12
I have an array of product specs, declared as type Variant. The 5th column is the price. I am trying to find product pairs according to a set of criteria, including pairs both priced above 6. I am getting runtime error 13, type mismatch in the colored part of below code excerpt:

Rich (BB code):
Dim ws As Worksheet: Set ws = ActiveSheet
    Dim rg As Range: Set rg = ws.Range("B2:F4110")
    Dim rCount As Integer: rCount = rg.Rows.count
    Dim rOffset As Integer: rOffset = rg.Row - 1
    Dim linenum As Integer
    Dim spread As Variant
    Dim ri As Integer
    Dim rj As Integer
    Dim min As Variant
   
   
    Dim Data() As Variant: Data = rg.Value
    linenum = 2
   
    Range("K:N").ClearContents
   
    For ri = 1 To rCount - 1
        For rj = ri + 1 To rCount
            min = IIf(Data(ri, 5) < Data(rj, 5), Data(ri, 5), Data(rj, 5))
            If Data(ri, 1) = Data(rj, 1) _
                    And Data(ri, 2) = Data(rj, 2) _
                    And min >= 6 _
                    And Abs(Data(ri, 5) - Data(rj, 5)) = 2 Then
                        Cells(linenum, 11).Value = Data(ri, 1)
                        Cells(linenum, 12).Value = Data(ri, 3)
                        Cells(linenum, 13).Value = Data(ri, 4)
                            If Data(ri, 5) < Data(rj, 5) Then
                                spread = Data(rj, 3) - Data(ri, 3)
                            Else
                                spread = Data(ri, 3) - Data(rj, 3)
                            End If
If i remove all references to min (i.e. I relax this condition), the code works. I have tried declaring min as type integer as well (which column 5 is) but I'm getting the same error.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
When the error occurs, what are the actual values of min and the relevant array items?
 
Upvote 0
I think I may have figured it out, I derived column 5 from another column that had a $ sign preceding the number, so I just used the left function to get the digits after the $ sign. I guess it turned it into a string whereas min was data type integer? Although I would have expected variant to catch that. Will try to copy and paste column 5 as values to see if that works and get back to you.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,221,631
Messages
6,160,942
Members
451,679
Latest member
BlueH1

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