简体   繁体   中英

Prevent Private sub from calling function - Excel VBA

I have now come to the understanding that my Private sub calls my UDF because my UDF has Application.Volatile = True . I can therefore prevent it from happening, by setting it to Application.Volatile = False instead.

Problem

Without having Volatile set to True in my function, it won't update, which is key in my sheet. And as earlier mentioned, I would like my private sub to stop calling my function, since it pretty much stops my loop from happening.

Goal

I would therefore like to know, whether it is possible to prevent my Private sub from calling my function, since it is placed in very different areas.

My private sub inserts a new value in B19, whereas my function is placed in A2.

Thank you in advance

As requested here is the code:

Sub UpdateSheets()
Dim WS_count As Integer
Dim I As Integer
Dim sht As Worksheet

Today = Date

WS_count = ActiveWorkbook.Worksheets.Count

For I = 1 To WS_count
    If I = 1 Then
        Else
        Set sht = Sheets(I)
            LnLAddress = sht.Range("A:A").Find("Lease end lessee:", , LookIn:=xlValues).Address(False, False, xlA1)
            LnLOff = sht.Range(LnLAddress).Offset(0, 1).Address(False, False, xlA1)
            LnLVal = sht.Range(LnLOff).Value
            NtceAddress = sht.Range("A:A").Find("Notice:", , LookIn:=xlValues).Address(False, False, xlA1)
            NtceOff = sht.Range(NtceAddress).Offset(0, 1).Address(False, False, xlA1)
            NtceVal = sht.Range(NtceOff).Value
            On Error GoTo Ending:
            NtceVal = Left(NtceVal, Application.WorksheetFunction.Find(" ", NtceVal) - 1)
            LnLVal = DateSerial(Year(LnLVal), Month(LnLVal) - NtceVal, Day(LnLVal))
            LnLYear = Year(LnLVal)
            On Error GoTo 0
                If LnLVal <= Today Then
                    AutoExtAddress = sht.Range("A:A").Find("Automatical extension of contract", , LookIn:=xlValues).Address(False, False, xlA1)
                    AutoExtOff = sht.Range(AutoExtAddress).Offset(0, 1).Address(False, False, xlA1)
                    AutoExtVal = sht.Range(AutoExtOff).Value
                    AutoExt = Left(AutoExtVal, Application.WorksheetFunction.Find(" ", AutoExtVal) - 1)
                    LnLNewVal = DateSerial(Year(LnLVal) + AutoExt, Month(LnLVal) + NtceVal, Day(LnLVal))
                    Application.Calculation = xlCalculationManual
                    sht.Range(LnLOff).Value = LnLNewVal
                    Application.Calculation = xlCalculationAutomatic 'loop through functions starts here...
                End If
    End If
Ending:
On Error GoTo 0
Next I

End Sub

And here the functions:

Function SHEETNAME(number As Long) As String
Application.Volatile True
    SHEETNAME = Sheets(number).Name
End Function

Function NxtShtNm(number As Long) As String
Application.Volatile True
    NxtShtNm = ActiveWorkbook.Sheets(ActiveSheet.Index + number - 1).Name
End Function

later on came to know a better solution, without making UDF's... I deleted my functions, since they only made problems within my sheet.

Then I made a named range, from the name manager, calling it "sheetlist"
After this I assigned this formula to what it refers to: =REPLACE(GET.WORKBOOK(1);1;FIND("]";GET.WORKBOOK(1));"")&T(NOW())

Making a formula in the cells, where I need to reference the workbooks: =IFERROR(HYPERLINK("#'" & INDEX(sheetlist;ROW()) & "'!A1";INDEX(sheetlist;ROW()));"")

Now it references the sheets in order, and assigning:
Private Sub WorkSheet_Activate() ActiveWorksheet.Calculate End sub
to the overview sheet, has made the trick of updating itself :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM