重新计算自定义VBA函数,仅在请求

我的问题是这样的:

我想在电子表格中使用一个自定义的Ribbon命令button,甚至是一个简单的命令button来初始化一个OLEDB数据库连接,并更新/重新计算所有需要这种连接的用户定义的函数,或者我指定的那些连接。 除了点击特定button时,我不希望这些function中的任何一个重新计算。 我有困难搞清楚如何做到这一点。 请提供您的帮助或build议。

请参阅下面的细节我做了什么:

我目前在一个访问数据库中存储数据,我使用Excel中的VBA进行特定的查询。 我已经将每个数据请求例程embedded到名为[fnc]的模块下的一组function中。 然后,我将它们作为用户定义的函数从Excel电子表格中进行访问。 这里给出一个例子:

Function ValueV(mm As String, yy As String, qtable As String, qcode As String, compare_period As Integer, average_period As Integer, weight As Boolean) As Variant 'Month Value Formula for Horizontal Data 'mm - month value 2-digit 'yy - year value 4-digit 'qtable - query table name eg. "cpia" 'qcode - query code for variable eg. "all0100" 'avgperiod - lag periods to average in calculation eg. 3-avgperiods for quarterly measure, 1-avgperiod for point measure. 'weight - boolean (true or false) value for weighting values given reference weight. Currently unsupported. Code should be extended to include this feature. (space holder for now) Dim lag_value As Variant Dim cur_value As Variant lag_value = 0 cur_value = 0 'STEP-A: Gets the initial Value average or not. '=============================================================== If compare_period > 0 Then 'Use this step to pickup initial value when compare_period <> 0 which requires a % change as opposed to a point value. 'Average_period must be greater than or equal to one (1). One (1) represents the current month which is the same as a point value. lmm = fnc.lagdate(mm, yy, compare_period, "mm") 'lag month (a single month for mValueH) lyy = fnc.lagdate(mm, yy, compare_period, "yy") 'lag year (a single month for mValueH) smm = fnc.lagdate(mm, yy, compare_period + average_period - 1, "mm") 'dating backwards to account for average period syy = fnc.lagdate(mm, yy, compare_period + average_period - 1, "yy") 'dating backwards to account for average period 'note, for smm & syy, the average period includes the lmm so we add back one (1) 'eg. 3-mth average is not 3-lags but current and 2-lags. sdate1 = syy & fnc.numtext(smm) 'start date for query (begining of lag value including average period) Set MyRecordset = New ADODB.Recordset MySql = sql.sqlVSers(lmm, lyy, qtable, qcode, sdate1) 'MsgBox (MySql) MyRecordset.Open MySql, MyConnect, adOpenStatic, adLockReadOnly Do Until MyRecordset.EOF 'Loop to end and enter required values lag_value = lag_value + MyRecordset(qcode) MyRecordset.MoveNext Loop 'Stop lag_value = lag_value / average_period MyRecordset.Close End If 'STEP-B: Gets the current Value average or not. '=============================================================== smm = fnc.lagdate(mm, yy, average_period - 1, "mm") 'dating backwards to account for average period syy = fnc.lagdate(mm, yy, average_period - 1, "yy") 'dating backwards to account for average period sdate1 = syy & fnc.numtext(smm) 'start date for query (begining of lag value including average period) Set MyRecordset = New ADODB.Recordset MySql = sql.sqlVSers(mm, yy, qtable, qcode, sdate1) MyRecordset.Open MySql, MyConnect, adOpenStatic, adLockReadOnly Do Until MyRecordset.EOF 'Loop to end and enter required values cur_value = cur_value + MyRecordset(qcode) MyRecordset.MoveNext Loop cur_value = cur_value / average_period MyRecordset.Close 'STEP-C: Calculates the Requested % Change or Point Value. '=============================================================== If compare_period = 0 Then ValueV = cur_value Else ValueV = cur_value / lag_value * 100 - 100 End If End Function 

由于我完全绕过了一个子例程的使用,所以与数据库的连接目前是作为工作簿帮助例程完成的,如下所示。

 Private Sub Workbook_AfterSave(ByVal Success As Boolean) Dim filePath filePath = ThisWorkbook.Path If Right$(filePath, 1) <> "\" Then filePath = filePath & "\" MyConnect = "Provider=Microsoft.ACE.OLEDB.12.0;" & _ "Data Source=" & filePath & "rsdata.accdb;" End Sub 

问题是,这个更新过程并不理想。 理想情况下,我想在菜单栏中定位一个自定义button(点击它)将连接到数据库,并重新计算在给定的工作表或工作簿中使用的所有用户定义的函数。

请提出您的build议或指出之前可能已经完成的事情。

提前致谢。 JR。

您正在尝试将UDF用于某些他们没有devise的function。 他们devise的行为就像其他单元格公式一样,当Excel决定他们需要的时候就要进行计算。

你有两个select

  • 重新devise你的应用程序不要使用UDF (IMO最好的方法)
  • 修改您的UDF s只响应您指定的触发器,例如button点击(海事组织一个cludge和一般坏主意)

如何重新devise以避免UDF取决于您的OP中未披露的因素