Excel VBA 类型不匹配错误将范围传递给数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25234592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:10:48  来源:igfitidea点击:

Excel VBA Type Mismatch Error passing range to array

arraysexcelvbatype-mismatch

提问by MikeG

I'm trying to retrieve the values of an array of cells in a worksheet (stored as an array rather than simple cells), but for some reason keep getting a Run-Time Error 13 Type Mismatch. I've read posts about similar issues, but many of those seem to have to do with either the array being of the wrong type (ie NOT Variant type), or being of static size.

我正在尝试检索工作表中单元格数组的值(存储为数组而不是简单的单元格),但由于某种原因,不断收到运行时错误 13 类型不匹配。我读过有关类似问题的帖子,但其中许多似乎与数组类型错误(即非变体类型)或静态大小有关。

Here is the relevant line where the error occurs in debugging:

这是调试中发生错误的相关行:

Dim SizeSelection() As Variant
SizeSelection = Workbooks("Wheels.xlsx").Worksheets("Test").Range("B1:W1")

I've also tried using

我也试过使用

Dim SizeSelection() As Variant
SizeSelection = Array(Workbooks("Wheels.xlsx").Worksheets("Test").Range("B1:W1"))

but still get the same error. Other than the 2 common mistakes mentioned above, does anyone have any idea why i would be getting a type mismatch? I've tried removing the array and storing the values in regular cells, but that made no difference.

但仍然得到同样的错误。除了上面提到的 2 个常见错误之外,有没有人知道为什么我会遇到类型不匹配?我尝试删除数组并将值存储在常规单元格中,但这没有区别。

Oh, I should mention that the data I'm trying to retrieve is in another workbook (though that should be clear from the code above) and that workbook is already open.

哦,我应该提到我试图检索的数据在另一个工作簿中(尽管从上面的代码中应该很清楚)并且该工作簿已经打开。

Thanks! Mike

谢谢!麦克风

回答by stobin

Change Dim SizeSelection() As Variantto Dim SizeSelection As Variantand you should be ok.

更改Dim SizeSelection() As VariantDim SizeSelection As Variant,你应该没问题。