[英]VBA function causing runtime error 13 while returning array in excel 2016 but not in office 365?
I have a function that reads data in an tab and returns it as an 1D array:我有一个 function 读取选项卡中的数据并将其作为一维数组返回:
Public Function OneDimension(arr)
OneDimension = Application.Transpose(Application.Transpose(arr))
End Function
the function is called when the form is opened:打开表单时调用 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
The data looks like this:数据如下所示:
user id![]() |
name![]() |
date![]() |
answer![]() |
amount![]() |
comments![]() |
completed![]() |
helper![]() |
---|---|---|---|---|---|---|---|
1 ![]() |
test,t![]() |
05/22/2022 ![]() |
yes![]() |
0.01 ![]() |
something![]() |
No![]() |
144687 ![]() |
with the formula用公式
helper = user id & date
user id
is a text field, and date
is stored as mm/dd/yyyy
, when I run it with Office 365 it seems fine, but when my boss ran it with excel 2016 it gave this error: user id
是一个文本字段, date
存储为mm/dd/yyyy
,当我用 Office 365 运行它时它看起来很好,但是当我的老板用 excel 2016 运行它时它给出了这个错误:
runtime error 13 type mismatch
with this in the debugger:在调试器中使用这个:
What could be causing this?是什么原因造成的?
Why use one-liners with limited functionality?为什么要使用功能有限的单线? They are shorter but usually not faster.
它们更短但通常不会更快。
In your sub, you could use:在你的子中,你可以使用:
actionDict(id).Add OneDaRow(rw.Cells(2).Resize(1, numArrCols))
The Function 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
A Function Test 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.