Custom Funtions

jonc31

Board Regular
Joined
Aug 20, 2009
Messages
83
I am really confused at this point. I created a custom function in vba that goes to another tab in the same workbook and grabs a correspoding number based off the statements in the function. The thing is when I intially enter the formula it is grabbing the correct data but if I update the tab the next day the formula is not updating with the new value.

How do you get custom functions to continually recalculate?
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Can you please post the code for the UDF (User Defined Function) you are using?
 
Upvote 0
Function GetLocalBalancePru(TabName As String, AcctN As String, Curr As String) As Double
Dim lngLastRow As Long
Dim intCell As Integer
With ThisWorkbook.Worksheets(TabName)
lngLastRow = .Range("A1").End(xlDown).Row
intCell = 2
Do Until intCell > lngLastRow
If .Range("B" & intCell).Value = AcctN Then
If .Range("C" & intCell).Value = "USD" Then
Exit Do
ElseIf .Range("C" & intCell).Value = Curr Then
GetLocalBalancePru = .Range("D" & intCell).Value
Exit Do
End If
End If
intCell = intCell + 1
Loop
End With

End Function
 
Upvote 0
Add to the first line of code:
Code:
Application.Volatile
 
Upvote 0
The problem is that there is nothing in the function that is being changed. A workaround would be to add Application.Volatile to the function. This way it recalculates anytime there is any calculations done to the worksheet:

Code:
Function GetLocalBalancePru(TabName As String, AcctN As String, Curr As String) As Double
Dim lngLastRow As Long
Dim intCell As Integer
[B][COLOR=red]Application.Volatile
[/COLOR][/B]With ThisWorkbook.Worksheets(TabName)
    lngLastRow = .Range("A1").End(xlDown).Row
    intCell = 2
    Do Until intCell > lngLastRow
        If .Range("B" & intCell).Value = AcctN Then
            If .Range("C" & intCell).Value = "USD" Then
                Exit Do
            ElseIf .Range("C" & intCell).Value = Curr Then
                GetLocalBalancePru = .Range("D" & intCell).Value
                Exit Do
            End If
        End If
        intCell = intCell + 1
    Loop
End With
End Function

However, I would not recommend this. It looks like you are using this function to effectively do a dual-condition lookup. Try, instead, using an array-entered INDEX/MATCH function:

=INDEX(TabName!D:D,MATCH(1,IF(TabName!B:B=AcctN,IF(TabName!C:C=Curr,1,"")),0))

Confirm entry with CTRL+SHIFT+ENTER (Not just enter) to get {brackets} around the formula. Note that TabName, AcctN, and Curr are variables you need to explicitly define in the formula.
 
Upvote 0
The problem is that there is nothing in the function that is being changed. A workaround would be to add Application.Volatile to the function. This way it recalculates anytime there is any calculations done to the worksheet:

Code:
Function GetLocalBalancePru(TabName As String, AcctN As String, Curr As String) As Double
Dim lngLastRow As Long
Dim intCell As Integer
[B][COLOR=red]Application.Volatile[/COLOR][/B]
With ThisWorkbook.Worksheets(TabName)
    lngLastRow = .Range("A1").End(xlDown).Row
    intCell = 2
    Do Until intCell > lngLastRow
        If .Range("B" & intCell).Value = AcctN Then
            If .Range("C" & intCell).Value = "USD" Then
                Exit Do
            ElseIf .Range("C" & intCell).Value = Curr Then
                GetLocalBalancePru = .Range("D" & intCell).Value
                Exit Do
            End If
        End If
        intCell = intCell + 1
    Loop
End With
End Function

However, I would not recommend this. It looks like you are using this function to effectively do a dual-condition lookup. Try, instead, using an array-entered INDEX/MATCH function:

=INDEX(TabName!D:D,MATCH(1,IF(TabName!B:B=AcctN,IF(TabName!C:C=Curr,1,"")),0))

Confirm entry with CTRL+SHIFT+ENTER (Not just enter) to get {brackets} around the formula. Note that TabName, AcctN, and Curr are variables you need to explicitly define in the formula.

Where would I put that index match function in excel or vba?
 
Upvote 0
It would be in Excel, in place of the VBA function, and I must make a correction. Array entered formulas cannot have entire column references. Try:

=INDEX(TabName!D1:D1000,MATCH(1,IF(TabName!B1:B1000=AcctN,IF(TabName!C1:C1000=Curr,1,"")),0))

If the last row of your data changes often, we can create a few named ranges to accomodate for this.
 
Upvote 0
However, I would not recommend this. It looks like you are using this function to effectively do a dual-condition lookup. Try, instead, using an array-entered INDEX/MATCH function:

=INDEX(TabName!D:D,MATCH(1,IF(TabName!B:B=AcctN,IF(TabName!C:C=Curr,1,"")),0))

Confirm entry with CTRL+SHIFT+ENTER (Not just enter) to get {brackets} around the formula. Note that TabName, AcctN, and Curr are variables you need to explicitly define in the formula.

MrK, Looking at the UDF, Tabname!D:D would need to be INDIRECT("'"&Tabname&"'!D:D") or similar, likewise for the other instances of Tabname, defeating the object of trying to make a non-volatile formula.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,671
Members
452,937
Latest member
Bhg1984

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