【问题标题】:VBA Type Mismatch of the same functions in different workbooks不同工作簿中相同功能的VBA类型不匹配
【发布时间】:2018-01-01 04:35:32
【问题描述】:

在不同的工作簿中运行相同的代码时,我遇到了解决类型不匹配的问题。假设工作簿 1 是原始工作簿,工作簿 2 是新工作簿。

工作簿 1 和 2 具有相同的代码(如下),其中 Listbox_Refresh 子调用 GetAccountRef() 函数。该代码在 Workbook 1 中运行良好,但在 Workbook 2 中类型不匹配,我无法弄清楚原因。

我检查了两个工作簿中GetAccountRef()VarTypes,它们是不同的。

对于工作簿 1

  • 这会按预期产生 8204 (vbArray + Variant):

    Debug.Print VarType(GetAccountRef())
    
  • 这会如预期的那样产生 8(字符串):

    Debug.Print VarType(GetAccountRef(0))
    

对于工作簿 2

  • 这导致 0(空):

    Debug.Print VarType(GetAccountRef())
    
  • 这会导致错误类型不匹配:

    Debug.Print VarType(GetAccountRef(0))
    

我要运行的功能是:

Function GetAccountRef() As Variant
On Error Resume Next

Dim Cell As Range
Dim Row_I As Range
Set Row_I = Sheet5.Range("9:9")    '<- ERROR: This range does not contain "Date"

Dim Counter As Integer
Counter = 0
Dim Date_Ref() As Variant


For Each Cell In Row_I
    If Cell = "Date" Then

        ReDim Preserve Date_Ref(Counter)
        Date_Ref(Counter) = Cell.Address
        GetAccountRef = Date_Ref

        Counter = Counter + 1
    End If
Next Cell


On Error GoTo 0
End Function

我正在尝试在 For 循环中使用此功能,如下所示:

    Dim ListedBnk As Variant
    For Each ListedBnk In GetAccountRef()
        ListedBnk = Replace(ListedBnk, "9", "7")
        .ComboBox1.AddItem Range(ListedBnk)
        .ComboBox2.AddItem Range(ListedBnk)

    Next ListedBnk

谢谢!

【问题讨论】:

  • 删除 On Error Resume Next - 它在哪里以及如何失败?
  • 由于 GetAccountRef 的值最终取自 Row_I 范围,我怀疑 Workbook2 中的范围为空。无论如何,我同意上面的评论,即应该删除错误捕获,并且您应该单步执行代码以查看失败的确切位置。
  • @TonyM 是的,确实是这样,希望我早点看到你的评论!

标签: vba excel


【解决方案1】:

发现我的错误,函数在不包含它的范围内查找标识符。感谢所有发表评论/解决方案的人!

改进将是编写动态标识符,以便在添加行/列时调整范围。

功能:

Function GetAccountRef() As Variant
On Error Resume Next

Dim Cell As Range
Dim Row_I As Range
Set Row_I = Sheet5.Range(**"10:10"**)    '<- previous range("9:9") did not contain the *identifier "Date"*, it was in row 10.

Dim Counter As Integer
Counter = 0
Dim Date_Ref() As Variant


For Each Cell In Row_I
    If Cell = "Date" Then

        ReDim Preserve Date_Ref(Counter)
        Date_Ref(Counter) = Cell.Address
        GetAccountRef = Date_Ref

        Counter = Counter + 1

    End If
Next Cell



On Error GoTo 0
End Function

工作表子:

   With Activesheet
        Dim ListedBnk As Variant
        For Each ListedBnk In GetAccountRef()
            ListedBnk = Range(Replace(ListedBnk, "10", "8"))    '<- Also needs to refer to **Row 10**
            .ComboBox1.AddItem ListedBnk
            .ComboBox2.AddItem ListedBnk

        Next ListedBnk
    End With

【讨论】:

    【解决方案2】:

    你的函数有错误。

    Function GetAccountRef() As Variant
    On Error Resume Next
    
    Dim Cell As Range
    Dim Row_I As Range
    Set Row_I = Sheet5.Range("9:9")    'TFSA Tracker ONLY
    
    Dim Counter As Integer
    Counter = 0
    Dim Date_Ref() As Variant
    
    
    For Each Cell In Row_I
        If Cell = "Date" Then
    
            ReDim Preserve Date_Ref(Counter)
            Date_Ref(Counter) = Cell.Address
            Counter = Counter + 1
        End If
    Next Cell
    
            GetAccountRef = Date_Ref '<~~ At this moved.
    On Error GoTo 0
    End Function
    

    和您的工作表模块

    Sub test()
        Dim ListedBnk As Variant
        Dim myArray As Variant
    
        myArray = GetAccountRef
    
        With ActiveSheet
            .ComboBox1.Clear
            .ComboBox2.Clear
        'For Each ListedBnk In GetAccountRef()
        For Each ListedBnk In myArray
            ListedBnk = Replace(ListedBnk, "9", "7")
            .ComboBox1.AddItem Sheet5.Range(ListedBnk)
            .ComboBox2.AddItem Sheet5.Range(ListedBnk)
    
        Next ListedBnk
        End With
    End Sub
    

    【讨论】:

    • 将 GetAccountRef = Date_Ref 移动到函数末尾确实修复了 VarType 问题并将 VarType(GetAccountRef()) 识别为 8204,但无法将 VarType(GetAccountRef(0)) 识别为数组 - 它显示0. 由于 GetAccountRef() 为空,工作表模块更改导致循环错误。感谢您的评论,我发现了我的错误!会贴在下面..这很愚蠢..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多