0

In my workbook, when I change the worksheet that this function is used on, or use F9, the macro generates the desired value. When I am on another worksheet in the workbook and make a change, the macro recalculates the values on the worksheet using this function to a zero. And, when I save the workbook, or open it, the recalculation resets all cells using the function zero as well. If I make the worksheet that uses this function active, and press F9, the macro calculates to the values desired and re-populates the cells on the active worksheet and elsewhere in the workbook with the values as desired.

I don't understand why this function is working this way. I need to always recalculate to its value and not reset to zero. Assistance is greatly appreciated!

Thank you.

Public Function LastMoneyValue(rng As Range) As Single
    Dim v As Variant
    Dim count As Integer
    Dim i As Variant
    Dim row As Long
    Dim column As Long
    Dim aMoneyValues() As Variant
    Dim vLastMoneyValue As Variant
    Dim money As Single
    Dim counter As Long
    
    Application.Volatile
    
    count = rng.Cells.count
    row = rng.row
    column = rng.column
    counter = column       'use counter to increment through the array. Hold the column variable constant.
    
    'Note ranges should be an even number. In this case 26 representing 13 months of the POP.
    ' money = count / 2
    money = count
    ReDim aMoneyValues(money)
    
    'fill the money array with money. Increment the starting column by 1 the first time as the money is in the adjacent cell to the hours.
     For i = 0 To money - 1
        aMoneyValues(i) = Cells(row, counter).value
        'counter = counter + 2
        counter = counter + 1
    Next i
    
    'evaluate the money array. Reset the counter i.
    i = 0
    For i = LBound(aMoneyValues) To UBound(aMoneyValues)
        v = aMoneyValues(i)
        If v > 0 Or v < 0 Then
            vLastMoneyValue = aMoneyValues(i)
        End If
    Next i
    LastMoneyValue = vLastMoneyValue
End Function
Dan
  • 1
  • 1
  • 1
    `aMoneyValues(i) = Cells(row, counter).value` - since the worksheet the `Cells` are on is not qualified, this is implicitly working off the `ActiveSheet`. – BigBen Aug 07 '20 at 14:19
  • Better to just read the value of the range into a 2D array instead of looping like you are. – BigBen Aug 07 '20 at 14:20
  • 1
    Most likely you don't need `Application.Volatile` either. – BigBen Aug 07 '20 at 14:21
  • @BigBen Thank you. This helped me fix the function. This is my first post, so is there something I need to do to give you credit for the fix? – Dan Aug 09 '20 at 22:50
  • No, since the question is closed, but I appreciate the comment. – BigBen Aug 10 '20 at 01:00

0 Answers0