VBA错误13(键入不匹配)试图将项目添加到集合中
我有2个包含名称的范围取2个数组。我想创建一个仅在数组2中的数组1中的名称中的第三个数组。但是,当试图将值添加到 collection 时,不匹配类型错误。
这是整个代码:
Sub CrearArreglos()
'**Array2**
Dim Array2() As Variant
Dim iCountLI As Long
Dim iElementLI As Long
If IsEmpty(Range("B3").Value) = True Then
ReDim Array2(0, 0)
Else
iCountLI = (Sheets("Sheet2").Range("B3").End(xlDown).Row) - 2
iCountLI = (Range("B3").End(xlDown).Row) - 2
ReDim Array2(iCountLI)
For iElementLI = 1 To iCountLI
Array2(iElementLI - 1) = Cells(iElementLI + 2, 2).Value
Next iElementLI
End If
'**Array 1:**
Dim Array1() As Variant
Dim iElementLC As Long
Worksheets("Sheet1").Activate
Array1 = Worksheets("Sheet1").Range("BD4:BD10").Value
Dim v3 As Variant
Dim coll As Collection
Dim i As Long
'**Extracting values from Array 1 that are not contained in Array 2**
Set coll = New Collection
For i = LBound(Array1, 1) To UBound(Array1, 1)
If Array1(i, 1) <> 0 Then
'**This line below displays error 13 ↓
coll.Add Array1(i, 1), Array1(i, 1)
End If
Next i
For i = LBound(Array2, 1) To UBound(Array2, 1)
On Error Resume Next
coll.Add Array2(i, 1), Array2(i, 1)
If Err.Number <> 0 Then
coll.Remove Array2(i, 1)
End If
If coll.exists(Array2(i, 1)) Then
coll.Remove Array2(i, 1)
End If
On Error GoTo 0
Next i
ReDim v3(0 To (coll.Count) - 1)
'Adds collection items to a new array:
For i = LBound(v3) To UBound(v3)
v3(i) = coll(i + 1)
Debug.Print v3(i)
Next i
因此,这是显示错误13的行。如果我删除了第二个“ array1(i,1)”,则运行良好,但它仅保存从array1中保存所有值,并且似乎忽略了代码的其余部分和条件)
coll.Add Array1(i, 1), Array1(i, 1)
奇怪的是,此代码始终工作过去的两个范围在同一张纸上时,在过去。这次,我从不同的床单中拿出范围。我不知道这是否是问题,尽管对我来说没有意义。
感谢任何帮助。先感谢您!
I have 2 arrays taken from two ranges containing names. I want to create a 3rd array with ONLY the names in array 1 that are not in array 2. However there's a mismatch type error when trying to add values to a collection.
This is the whole code:
Sub CrearArreglos()
'**Array2**
Dim Array2() As Variant
Dim iCountLI As Long
Dim iElementLI As Long
If IsEmpty(Range("B3").Value) = True Then
ReDim Array2(0, 0)
Else
iCountLI = (Sheets("Sheet2").Range("B3").End(xlDown).Row) - 2
iCountLI = (Range("B3").End(xlDown).Row) - 2
ReDim Array2(iCountLI)
For iElementLI = 1 To iCountLI
Array2(iElementLI - 1) = Cells(iElementLI + 2, 2).Value
Next iElementLI
End If
'**Array 1:**
Dim Array1() As Variant
Dim iElementLC As Long
Worksheets("Sheet1").Activate
Array1 = Worksheets("Sheet1").Range("BD4:BD10").Value
Dim v3 As Variant
Dim coll As Collection
Dim i As Long
'**Extracting values from Array 1 that are not contained in Array 2**
Set coll = New Collection
For i = LBound(Array1, 1) To UBound(Array1, 1)
If Array1(i, 1) <> 0 Then
'**This line below displays error 13 ↓
coll.Add Array1(i, 1), Array1(i, 1)
End If
Next i
For i = LBound(Array2, 1) To UBound(Array2, 1)
On Error Resume Next
coll.Add Array2(i, 1), Array2(i, 1)
If Err.Number <> 0 Then
coll.Remove Array2(i, 1)
End If
If coll.exists(Array2(i, 1)) Then
coll.Remove Array2(i, 1)
End If
On Error GoTo 0
Next i
ReDim v3(0 To (coll.Count) - 1)
'Adds collection items to a new array:
For i = LBound(v3) To UBound(v3)
v3(i) = coll(i + 1)
Debug.Print v3(i)
Next i
So this is the line where error 13 is displayed. If I remove the second "Array1(i, 1)", it runs fine but it only saves all values from Array1 and it seems to ignore the rest of the code and the conditions)
coll.Add Array1(i, 1), Array1(i, 1)
Oddly enough, this code has always worked perfectly in the past when having both ranges in the same sheet. This time I'm taking the ranges from different sheets. I don´t know if that's the issue, although it doesn't make sense for me.
I'd appreciate any help. Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该与您打算做同样的事情,并且表现良好:
This should do the same thing as you intend, and with good performance: