repeatedly calling Custom Function as Formula in a cell unnecessarily

pradee

New Member
Joined
Sep 8, 2018
Messages
18
Hi guys,

I have Created Custom Function with three arguments and all three are range values from two different sheets.
the custom function is returning the double values and it is getting placed in the cell correctly,

Now the thing is, when i delete or perform other operations in the tool (With the other Sheets except those two), that custom function (which is a formula for a cell) is getting called unnecessarily which is increasing the execution time of the tool.

those cells are referring to other sheets in the cells, but am sure that it is not changing any values in the cells.

the strange thing what i noticed is, custom function (Formula in the cell) is getting called even i am deleting other sheets in the workbook (through code as well as manual deletion)

Please help me with this, it is increasing execution time of the tool

Thank You :)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello,

Just insert a Boolean variable at the top of your UDF ...

With True / False, you will always master when your UDF will run ... or not ... :wink:

HTH
 
Upvote 0
Hi,

Thanks for replay, i didn't mean to ask about Boolean explanation,
i was asking about how to use it in the custom formula.
bcz when i am deleting the sheet(through code or manually),
i cant change the boolean value in the custom formula, because that function is referring to particular cell as formula

thanks once again
 
Upvote 0
If the event you want to trap is the deletion of a sheet ....

Your boolean variable should return :

- True when the sheet exists

- False when the sheet does not exist

Hope this clairifies ...
 
Upvote 0
Specifically preventing trigger when you delete a sheet, try this method (I used a simple UDF to illustrate)

- declare public boolean variable Apply at top of module
- add the code in red to your function
- Message Box will show when the UDF triggers (delete this line after testing :) )


In module containing your UDF
Code:
[COLOR=#ff0000]Public Apply As Boolean[/COLOR]

Function Add3ranges(r1 As Range, r2 As Range, r3 As Range)
[COLOR=#ff0000]    If Apply = True Then
        Apply = False
        Exit Function
    End If[/COLOR]

   Application.Volatile
[COLOR=#ff0000]    MsgBox "trigger"
    Apply = False[/COLOR]
    Add3ranges = r1 + r2 + r3
    
End Function

In ThisWorkbook module
Code:
Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object)
    Apply = True
End Sub
 
Last edited:
Upvote 0
@Yongle

Specifically ... it seems to me OP wants the job to be done ... without even posting his UDF ....!!! :wink:
 
Upvote 0
@pradee
You may prefer to puzzle things out for yourself which is better in the long run. Hopefully the information provided has enabled you to solve your issue.

If you need any further help please post your UDF as suggested by @James006 - it makes it easier for us to re-create your situation.
thanks
 
Upvote 0
@Yongle and @James006

Sorry I couldn't post my code

Here is my UDF


Code:
Public Function SumValues(ByVal PairRng As Range, ByVal FileRng As Range, ByVal RowAddress As Range) As Double
    If Apply = True Then
        Apply = False
        Exit Function
    End If
    Application.Calculation = xlCalculationManual
    Application.Volatile
    'Apply = False
  Dim ColCount As Integer
    Dim NodRowCount As Integer
    Dim nodRow As Integer
    Dim equal As Boolean
    Dim wSheet As Worksheet
    Set wSheet = FileRng.Worksheet
    equal = False


    wSheet.Unprotect


    ncol = 0


    lastNodeColumn = Sheets("Pairing").Cells(2, Sheets("Pairing").Columns.Count).End(xlToLeft).Column
    lastNOdeRow = Sheets("Pairing").Cells(Sheets("Pairing").Rows.Count, 3).End(xlUp).Row
    ColCount = FileRng.Count
    NodRowCount = PairRng.Count
    For nodecolum = 3 To lastNodeColumn - 4
        For j = 4 To ColCount
            If equal = False Then
                For nodRow = 3 To lastNOdeRow
                    If (wSheet.Cells(4, j).Value) Like (Sheets("Pairing").Cells(nodRow, nodecolum).Value) Then


                        ncol = j
                        equal = True
                        Exit For
                    End If
                Next nodRow
            End If


            If equal = True Then
                If InStr(wSheet.Cells(5, j).Value, "Max-Amplitude") > 0 Then
                    nodeval = wSheet.Cells(4, ncol).Value
                    For qp = 3 To ColCount
                        If Sheets("Pairing").Cells(qp, nodecolum).Value = nodeval And Sheets("Pairing").Cells(qp, lastNodeColumn - 2).Value = "Yes" Then
                            found = True
                            Exit For
                        End If
                    Next qp
                    If found = True Then
                        sumValue = sumValue + wSheet.Cells(RowAddress.Row, j).Value
                        found = False
                    End If
                    ncol = 0
                    equal = False
                End If
            End If
        Next
    Next
    SumValues = sumValue
    Application.Calculation = xlCalculationAutomatic
End Function
@Yongle I applied your method that you have suggested, but the scenario is, when i delete sheet it is assigning the value of variable 'Apply' to True
and it is terminating the function.

the function which i have written is being used by other cells also, for the first cell (or first call) it is exiting the function
but for the next cell (or next call) the value of variable Apply is set to "False" and it is recalculating everything

i hope you understood the scenario
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,629
Members
452,661
Latest member
Nonhle

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