Building Custom Function with Array Loop

MojoJojo2023

New Member
Joined
Aug 7, 2023
Messages
9
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hi all!

I am attempting to build a custom function to pass values to an array and return a value based on whether criteria matches the passed values. I am not new to VBA, but am trying to learn utilize arrays to process more in memory. Any feedback on the issue with the below would be appreciated. Thanks in advance! :


VBA Code:
Function NP(term As Integer, dte As Date, rng As Range) As Double

Dim tmparray As Variant
Dim i As Long, j As Long

tmparray = rng

For i = LBound(tmparray, 1) To UBound(tmparray, 1)
    For j = LBound(tmparray, 1) To UBound(tmparray, 15)
        If tmparray(i, 1).Value = dte And tmparray(i, 3).Value = "Yes" And tmparray(i, 7).Value = term Then
        NP = tmparray(i, 12)
        End If
    Next j
    
Next i

End Function
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi, you didn't really explain what the actual issue was, but it's not clear what you are trying to do with the J for loop and you don't use .value to reference the contents of the array items.

Maybe give this a go.

VBA Code:
Function NP(term As Integer, dte As Date, rng As Range) As Double

Dim tmparray As Variant

Dim i As Long
tmparray = rng

For i = LBound(tmparray, 1) To UBound(tmparray, 1)
    If tmparray(i, 1) = dte And tmparray(i, 3) = "Yes" And tmparray(i, 7) = term Then
        NP = tmparray(i, 12)
    End If
Next i

End Function
 
Upvote 0
Sorry for the lack of clarity - I am trying to create something reminiscent of a binary search Xlookup function over a two-dimensional array. The column count will always have an upper bound of 15, but the rows may be variable depending on data used at runtime.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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