代码之家  ›  专栏  ›  技术社区  ›  QHarr

更改传递给函数的参数类型时类型不匹配

  •  0
  • QHarr  · 技术社区  · 8 年前

    情况:

    我有一个功能, RemoveEmptyArrRowCol ,它接受两个参数,其中一个是数组( tempArr ).

    当第二个论点是 长的 一切都很好。当我把第二个参数改为 一串 (和相关的调用变量)我得到:

    类型不匹配(错误13)

    因此,在下面的代码示例中:

    1. Test1 运行良好
    2. Test2 失败

    问题:

    1) 为什么两者的行为有所不同?

    2) 如何修复第二个版本,使其按照第一个版本运行?

    3) 当我传递字典值(数组)作为第一个参数,而不是直接从工作表中读取时,我将如何进一步证明这一点?

    我所尝试的:

    这似乎是一个常见的问题,我已经研究了其中一些问题;我把其中一些放在这个问题的底部作为参考。然而,我仍然没有解决为什么这两个子系统中的第一个子系统工作,而第二个子系统没有工作?

    我尝试了以下不同的组合:

    1. 添加额外括号
    2. 显式声明 tempArr公司 : Dim tempArr() As Variant
    3. 更改函数签名的部分: ByRef tempArr() As Variant

    在看了@Fionnuala对这个问题的回答后, MS Access/VBA type mismatch when passing arrays to function/subroutine ,我决定尝试使用 Call :

    Call RemoveEmptyArrRowCol2(ws.Range("C4:I129").Value, tempStr)
    

    这已编译,但意味着我需要更改代码的其他部分,以确保 tempArr公司 已正确填充。如果我这样做,我还可以将函数转换为过程。

    按原样,流程是我填充的 tempArr公司 ,在测试示例中,直接从板材开始,然后移交给另一个子零件,即。

    tempArr = RemoveEmptyArrRowCol(ws.Range("C4:I129").Value, tempStr)
    ArrayToSheet wb.Worksheets("Test").Range("A1"), tempArr
    

    注:问题3:

    在最终版本中,我将传递一个从字典中提取的数组,作为第一个参数,即。

    tempArr = RemoveEmptyArrRowCol( ArrayDict(tempStr), tempStr)
    

    工作版本:

    Option Explicit
    
    Public Sub Test1()
    
        Dim tempArr() 'variant
    
        Init
    
        Dim tempStr As String: tempStr = "Response Times"
    
        tempArr = RemoveEmptyArrRowCol(ws.Range("C4:I129").Value, categoryDict(tempStr & "Cols"))
    
    End Sub
    
    Private Function RemoveEmptyArrRowCol(ByRef tempArr As Variant, ByVal nCols As Long) As Variant
    
    End Function
    

    失败版本:

    Public Sub Test2()
    
        Dim tempArr()
    
        Init
    
        Dim tempStr As String: tempStr = "Response Times"
    
        tempArr = RemoveEmptyArrRowCol2(ws.Range("C4:I129").Value, tempStr)
    
    End Sub
    
    Private Function RemoveEmptyArrRowCol2(ByRef tempArr As Variant, ByVal tempStr As String) As Variant
    
    End Function    
    

    当前完整功能的示例:

    Private Function RemoveEmptyArrRowCol(ByRef tempArr As Variant, ByVal tempStr As String) As Variant
    
        Dim i As Long
        Dim j As Long
        Dim counter As Long
        counter = 0
    
        Dim tempArr2()
    
        Dim totCol As Long
        Dim adjColTotal As Long
    
        totCol = categoryDict(tempStr & "Cols")
        adjColTotal = categoryDict(tempStr & "ColsAdj")
    
        Select Case tempStr
    
            Case "ResponseTimes", "NoCCPR"
    
                ReDim tempArr2(1 To 1000, 1 To adjColTotal)
    
                For i = 1 To UBound(tempArr, 1)
    
                    If tempArr(i, 2) <> vbNullString Then   'process row
    
                        counter = counter + 1                'load row to temp array (counter becomes row count)
    
                        For j = 1 To totCol
    
                            Select Case j
                                Case Is < 4
                                    tempArr2(counter, j) = tempArr(i, j)
                                Case Is > 4
                                    tempArr2(counter, j - 1) = tempArr(i, j)
                            End Select
    
                        Next j
    
                    End If
    
                Next i
    
                RemoveEmptyArrRowCol = RedimArrDimOne(tempArr2, adjColTotal, counter)
    
           Case "Incidents"
    
        End Select
    
    End Function
    

    其他参考资料:

    1) Passing arrays to functions in vba

    2) Passing array to function returns compile error

    3) Type mismatch error when passing arrays to a function in excel vba

    4) Should I use Call keyword in VBA

    2 回复  |  直到 8 年前
        1
  •  1
  •   Vityata    8 年前

    这实际上取决于您的输入和输出,例如removemptyarrowcol2函数中的内容。这是一个选项,其中 tempStr as String 未失败:

    Public Sub Test2()
        Dim tempArr()
        Dim tempStr As String: tempStr = "Response Times"
        tempArr = RemoveEmptyArrRowCol2(Range("C4:I129").Value, tempStr)
    End Sub
    
    Private Function RemoveEmptyArrRowCol2(ByRef tempArr As Variant, _
                                           ByVal tempStr As String) As Variant
        RemoveEmptyArrRowCol2 = Array(1, 2)
    End Function
    

    E、 例如,如果删除返回值(数组(1,2),它将失败),但它应该失败,因为它不返回任何内容。

        2
  •  1
  •   Dirk Kampfmeier    8 年前

    将“Dim tempArr As Variant”定义为变量数组,而不是使用“()”定义为变量数组

    请向我们展示您的功能“categoryDict”和“ArrayDict”

    “呼叫”不是必须的!

    您可以按如下方式访问值:

    Dim r As Long
    Dim c As Long
    For r = 1 To UBound(tempArr, 1)
        For c = 1 To UBound(tempArr, 2)
            Debug.Print tempArr(r, c)
        Next
    Next