从Excel中的静态变量更新UDF中的值?

时间:2014-11-25 17:28:03

标签: excel vba excel-vba

我在UDF中有一个静态哈希表来跟踪不同工作表中的某些值。

Public Function config_found_in_range(what As String, in_range As Range, Optional debug_rst As Boolean = False) As Long
    Dim row_num As Long
    Dim parent_inst As Long
    Dim counter As Long
    Dim caller_rng As Range
    Dim in_range_name As String
    Static config_hash_table As Variant
    If debug_rst Then
        config_hash_table = Nothing
        Exit Function
    End If
    If IsEmpty(config_hash_table) Then
        Set config_hash_table = CreateObject("Scripting.Dictionary")
    ElseIf config_hash_table Is Nothing Then
        Set config_hash_table = CreateObject("Scripting.Dictionary")
    End If

    Set caller_rng = Application.Caller.Parent.Cells
    row_num = Application.Caller.Row
    parent_inst = caller_rng.Range(LibCommon.FindColumn(ColumnHeaders.PARENTINSTANCETWO) & row_num).Value2
    in_range_name = in_range.Parent.Name

    If vbNullString = what Then
        counter = 0
    Else
        counter = Application.WorksheetFunction.CountIf(in_range, what)
    End If
    If config_hash_table.Exists(parent_inst & in_range_name) Then
        config_hash_table(parent_inst) = config_hash_table(parent_inst & in_range_name) + counter
    Else
        config_hash_table.Add parent_inst & in_range_name, counter
    End If
    config_found_in_range = config_hash_table(parent_inst & in_range_name)
End Function

每当哈希表更新时,只有后续调用才能看到新值。

有没有办法让所有调用UDF的单元格都能在哈希表中看到新值?

0 个答案:

没有答案