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 :)
 
your function is rather complex :confused:
- will look later in the week when I have time
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You made the function volatile (which you have to because of the way it's been written), so it will always recalculate when you change something.
 
Upvote 0
You said in post#1
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

and now you are saying...
@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.

I am confused - the function stops when a sheet is deleted - - have I misunderstood your requirements :confused: :confused:
 
Last edited:
Upvote 0
Hello Again, @Yongle i have fixed the issue with your suggestion, i have implemented it little more,
i have set the variable Apply = True only when user is going to change those three parameters (three ranges)

i have used change event, here is my code

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range
    Dim I As Integer
    Dim lnRow As Long, lnCol As Long
    lnRow = 2
    lnCol = Sheets("Node Pairing").Cells(lnRow, 1).EntireRow.Find(What:="Curve", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
    Set KeyCells = Range(Cells(2, lnCol), Cells(2, lnCol).End(xlDown))
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
        Is Nothing Then
            Apply = True
    End If
End Sub

Thanks for the suggestion :)
 
Upvote 0
@Yongle sorry for late reply,

Actually there there are multiple cells which are using the formula, when i delete the sheet, it will call 'Workbook_SheetBeforeDelete' and it will set Apply ="True"
and it will go for the custom function, and in the custom function==> since Apply="True" it will enter the condition and it will set Apply ="False" and it will exit the function
this is done for single cell.

for the next cell (which includes custom formula) since the value of variable Apply="False", it won't exit the function now, now it will recalculate everything once again

like wise i have dynamic number of cells which is using customised formula

I hope you understood my explanation :)
 
Upvote 0
Reply to post#16

Yes I understand and here is a suggestion

Change the condition in the UDF to
Code:
If Apply = True Then Exit Function

and then you need something to reset the value of Apply to False after the sheet has been deleted
- a button
- a macro triggered by a short cut
- an event
eg use sheet 1 and use the a DoubleClick event like this
Goes in sheet code window
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    Apply = False
End Sub
 
Last edited:
Upvote 0
Another approach would be to not use Boolean variable in the UDF but to set Calculation mode to MANUAL and then user recalcuates with {CTRL}{ALT}{F9} etc
 
Upvote 0

Forum statistics

Threads
1,215,634
Messages
6,125,938
Members
449,275
Latest member
jacob_mcbride

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