繁体   English   中英

VBA function 在 excel 2016 中返回数组时导致运行时错误 13 而不是在 Office 365 中?

[英]VBA function causing runtime error 13 while returning array in excel 2016 but not in office 365?

我有一个 function 读取选项卡中的数据并将其作为一维数组返回:

Public Function OneDimension(arr)
    OneDimension = Application.Transpose(Application.Transpose(arr))
End Function

打开表单时调用 function:

Set actionDict = New Scripting.Dictionary
numArrCols = Data.Columns.Count - 1
ReDim arr(1 To numArrCols) 'empty array

For Each rw In Data.rows
    id = "" & rw.Cells(1).Value
    If Not actionDict.Exists(id) Then
        actionDict.Add id, New Collection 'new key: add key and empty collection
    End If
    actionDict(id).Add OneDimension(rw.Cells(2).Resize(1, numArrCols).Value) 'add the row value as 1D array
Next rw

数据如下所示:

用户身份 名称 日期 回答 数量 注释 完全的 帮手
1个 测试,吨 05/22/2022 是的 0.01 某物 144687

用公式

helper = user id & date

user id是一个文本字段, date存储为mm/dd/yyyy ,当我用 Office 365 运行它时它看起来很好,但是当我的老板用 excel 2016 运行它时它给出了这个错误:

runtime error 13 type mismatch

在调试器中使用这个:

调试器错误

是什么原因造成的?

单行范围到一维数组

  • 为什么要使用功能有限的单线? 它们更短但通常不会更快。

  • 在你的子中,你可以使用:

     actionDict(id).Add OneDaRow(rw.Cells(2).Resize(1, numArrCols))

Function

Function OneDaRow(ByVal RowRange As Range) As Variant
    
    Dim Data As Variant ' 2D one-based one-row array
    Dim c As Long
    
    With RowRange.Rows(1)
        c = .Columns.Count
        If c = 1 Then ' one cell
            ReDim Data(1 To 1, 1 To 1): Data(1, 1) = .Value
        Else ' multiple cells
            Data = .Value
        End If
    End With
        
    Dim Arr As Variant: ReDim Arr(1 To c) ' 1D one-based array
    
    For c = 1 To c
        Arr(c) = Data(1, c)
    Next c
    
    OneDaRow = Arr

End Function

Function 测试

Sub OneDaRowTEST()
    Dim rg As Range: Set rg = Range("A1").CurrentRegion
    Debug.Print Join(OneDaRow(rg), vbLf)
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM