UDF – 确定是重新计算还是首次执行

我想编程一个UDF来查询访问数据库。 我想知道是否有可能有2个不同的工作stream程,这取决于它是第一次执行还是只重新计算UDF。
理想情况下,我将有一个UDF,您可以提供数据库的主键,UDF提供访问数据库表的可能值的概述。 如果这是一个重新计算,我不想有一个用户窗体再次popup。 这可能吗? 有人能指出我正确的方向吗?
谢谢!

编辑尝试显示一些(虚拟)代码:

public function key_from_table(primarykey as string) as string ' Read-out column names from Access table for userform ' Trigger userform with possible column names and let user choose ' readout Chosen column names key_from_table = Call get_from_db(Primary_key, column_names) end function Function get_from_db(Primarykey as string, column_names as string) as string 'call Access db and readout result end Function 

如果重新计算被触发,则用户窗体popup窗口再次出现
我仍然是新的Excel VBA – 请告诉我,如果这是相当愚蠢的:)

声明一个全局的字典variables。 就在触发表单之前,检查字典是否已经有列名。 如果是这样,不要触发表格。 如果没有,则触发表单并在表单closures后将列名添加到字典中。 您可以清除Workbook_BeforeClose的variables,以保持清洁

像这样的东西应该为你工作:

 Public pub_sRecalcCheck As String Public Function MyTest() As Boolean Dim bReCalc As Boolean If InStr(1, " " & pub_sRecalcCheck & " ", " " & Application.Caller.Address(External:=True) & " ", vbTextCompare) = 0 Then 'This is a brand new calculation 'Add this cell to the public variable storing where calculations for this UDF have occurred bReCalc = False pub_sRecalcCheck = WorksheetFunction.Trim(Replace(pub_sRecalcCheck, " " & rCell.Address(External:=True) & " ", " ")) '''''''''''''''''''''''''''''''''''''''''''' ' ' ' Your code here for new calculation ' ' ' '''''''''''''''''''''''''''''''''''''''''''' Else 'This is a recalculation bReCalc = True '''''''''''''''''''''''''''''''''''''''''' ' ' ' Your code here for recalcuations ' ' ' '''''''''''''''''''''''''''''''''''''''''' End If MyTest = bReCalc End Function 

编辑:以防万一公式从单元格中删除,使用ThisWorkbook模块中清除该单元格的地址从RecalcCheck公共stringvariables,以便如果新的公式被放在那里它被视为一个新的计算:

 Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Dim rCell As Range For Each rCell In Target.Cells 'Check if cell has been deleted/cleared If IsEmpty(rCell) Then 'Found deleted cell, check if it is stored for the recalc checks If InStr(1, " " & pub_sRecalcCheck & " ", " " & rCell.Address(External:=True) & " ", vbTextCompare) > 0 Then 'It is stored, remove it so that if formula is put back it is treated as a new calculation pub_sRecalcCheck = WorksheetFunction.Trim(Replace(pub_sRecalcCheck, " " & rCell.Address(External:=True) & " ", " ")) End If End If Next rCell End Sub