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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
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,224,846
Messages
6,181,304
Members
453,031
Latest member
Chris_1

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