UDF Array Help

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
In the following function, I want to get every column that says "Y" and sum everything until the next "Y". My function works properly. However, When there is a "N" at the beginnning, it will give a #VALUE! Which makes sense because my logic says if it doesn't = Y, then i = i + 1 and then ret(j - i, 0). This obviously will not work effectively if "N" is the first cell. How can I add to this logic so It will still work when "N" is the first cell?

VBA Code:
Function getY(Rng1 As Range, Rng2 As Range) As Variant
    Dim i As Integer, j As Integer, n As Integer
    Dim A As Variant, B As Variant, ret()
    n = Rng1.Rows.Count
    ReDim ret(1 To n, 0)
    A = Rng1.Value2
    B = Rng2.Value2
    For j = 1 To n
        If B(j, 1) = "Y" Then
            ret(j, 0) = A(j, 1)
            i = 0
        Else
            i = i + 1
            ret(j - i, 0) = ret(j - i, 0) + A(j, 1)
        End If
    Next j
    getY = ret
End Function
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How about
VBA Code:
Function getY(Rng1 As Range, Rng2 As Range) As Variant
    Dim i As Integer, j As Integer, n As Integer
    Dim A As Variant, B As Variant, ret()
    Dim Flg As Boolean
    n = Rng1.Rows.Count
    ReDim ret(1 To n, 0)
    A = Rng1.Value2
    B = Rng2.Value2
    For j = 1 To n
        If B(j, 1) = "Y" Then
         Flg = True
            ret(j, 0) = A(j, 1)
            i = 0
        ElseIf Flg Then
            i = i + 1
            ret(j - i, 0) = ret(j - i, 0) + A(j, 1)
        End If
    Next j
    getY = ret
End Function
 
Upvote 0
How about
VBA Code:
Function getY(Rng1 As Range, Rng2 As Range) As Variant
    Dim i As Integer, j As Integer, n As Integer
    Dim A As Variant, B As Variant, ret()
    Dim Flg As Boolean
    n = Rng1.Rows.Count
    ReDim ret(1 To n, 0)
    A = Rng1.Value2
    B = Rng2.Value2
    For j = 1 To n
        If B(j, 1) = "Y" Then
         Flg = True
            ret(j, 0) = A(j, 1)
            i = 0
        ElseIf Flg Then
            i = i + 1
            ret(j - i, 0) = ret(j - i, 0) + A(j, 1)
        End If
    Next j
    getY = ret
End Function
Hi Fluff! Thank you for the help! It gives me 0 all the way down
 
Upvote 0
Do you have any "Y" in the second range?
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,424
Members
448,961
Latest member
nzskater

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