How to fix an Excel UDF that calculates correctly on one line but reports the wrong info when flash filling

dan mcgovern 0 Reputation points
2023-01-20T11:49:31.01+00:00

Hi

Finally figured out how to write this UDF that calculates position in a range. problem is when the sheet is refreshed or a flash fill is done, then the UDF incorrectly makes all the values the same. since this is happening in a sheet, is there a way to make this work?

I am using this UDF to look at a column, find the string, make an array of the string with row and position, and return the position in the activecell.

Function GetArrayCount(StringRange As Range) As Long 'StringRange As Range, Seed As Range

    Dim Ws As Worksheet
    Dim lCounter As Long
    Dim LastRow As Long
    Dim CodWinCount As Long
    Dim Seed As Long
    Dim vArray() As Variant
    Dim GetCount As Variant 'this is the common count
    Dim GetRow As Variant 'this is the row value
    
    Set Ws = ThisWorkbook.Sheets("ControlsDB")
    LastRow = Ws.Cells(Ws.Rows.count, "A").End(xlUp).Row
    Seed = Ws.Range("Seed").Value
    CodWinCount = Seed - 1
    
    With Ws
        For lCounter = 2 To LastRow
            If .Range("F" & lCounter) = SCODEWINDOW Then
                CodWinCount = CodWinCount + 1
                ReDim Preserve vArray(Seed To CodWinCount)
                vArray(CodWinCount) = lCounter & " " & CodWinCount
            End If
        Next lCounter
    End With
    
    Application.EnableEvents = False
    
    For lCounter = LBound(vArray) To UBound(vArray)
        GetCount = Split(vArray(lCounter), " ")(1)
        GetRow = Split(vArray(lCounter), " ")(0)
        If ActiveCell.Row = GetRow Then
            GetArrayCount = GetCount
            Application.MoveAfterReturnDirection = False
            Exit Function
            'Stop
        End If
    Next lCounter
    
End Function
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,668 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,694 questions
0 comments No comments
{count} votes