尝试设置数组值时 VBA 中的类型不匹配
Type mismatch in VBA when trying to set array values
我正在尝试从 AutoCAD 导出并生成物料清单 (BOM)。输出文件为放置在模型中的每个项目都有一个条目(由于情有可原,我无法使用 AutoCAD 的内置 BOM 构建功能)。在制作 BOM 时,我需要没有重复的零件号和总数量。
首先,我将所有部件号添加到一个数组中。然后我使用一个函数(我没有写)来删除重复项。然后我尝试将我的原始零件号数组重新调整为 2D 和无重复数组的长度。接下来,我想将无重复数组中的所有值添加回第一列中的原始数组。后来我想到了如何对零件号求和并将它们与匹配的零件号一起添加到数组的第二列。
这是我得到的示例:
CPN: QTY:
5551 1
5552 3
5551 1
5551 1
5555 6
这是我需要的输出(排序无所谓)
CPN: QTY:
5551 3
5552 3
5555 6
这是我目前拥有的完整代码。我在 cpns(i, 1) = temp(i)
处收到错误
Sub consolidate()
Dim arrfirst As Integer, arrlast As Integer
Dim cpns() As Variant
Dim CPN_COUNT As Integer
Range("E1000").Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlUp)).Select
Range("E2:E4").Select
Range("E4").Activate
CPN_COUNT = Selection.Count
ReDim cpns(1 To CPN_COUNT)
For i = 1 To CPN_COUNT
cpns(i) = Cells(i + 1, 5)
Next
temp = ArrayRemoveDups(cpns)
arrfirst = LBound(temp)
arrlast = UBound(temp)
ReDim cpns(arrfirst To arrlast, arrfirst To arrlast)
For Each i In temp
cpns(i, 1) = temp(i)
Next
End Sub
Function ArrayRemoveDups(MyArray As Variant) As Variant
Dim nFirst As Long, nLast As Long, i As Long
Dim item As String
Dim arrTemp() As String
Dim Coll As New Collection
'Get First and Last Array Positions
nFirst = LBound(MyArray)
nLast = UBound(MyArray)
ReDim arrTemp(nFirst To nLast)
'Convert Array to String
For i = nFirst To nLast
arrTemp(i) = CStr(MyArray(i))
Next i
'Populate Temporary Collection
On Error Resume Next
For i = nFirst To nLast
Coll.Add arrTemp(i), arrTemp(i)
Next i
Err.Clear
On Error GoTo 0
'Resize Array
nLast = Coll.Count + nFirst - 1
ReDim arrTemp(nFirst To nLast)
'Populate Array
For i = nFirst To nLast
arrTemp(i) = Coll(i)
Next i
'Output Array
ArrayRemoveDups = arrTemp
End Function
感谢任何帮助。我确信有一种更简单的方法可以完成所有这些,但我是 VBA
的新手
我还应该在我的错误点添加 cpns 的数据类型是 Variant/Variant 并且 temp 的数据类型是 Variant/String.
数组应该使用 For i = Lbound(...) to Ubound(...)
进行迭代,而不是 For Each
。
改变
For Each i In temp
到
For i = Lbound(temp) to Ubound(temp)
正如评论中所指出的,您可以使用 For Each
循环...但是 只是不要。
我正在尝试从 AutoCAD 导出并生成物料清单 (BOM)。输出文件为放置在模型中的每个项目都有一个条目(由于情有可原,我无法使用 AutoCAD 的内置 BOM 构建功能)。在制作 BOM 时,我需要没有重复的零件号和总数量。
首先,我将所有部件号添加到一个数组中。然后我使用一个函数(我没有写)来删除重复项。然后我尝试将我的原始零件号数组重新调整为 2D 和无重复数组的长度。接下来,我想将无重复数组中的所有值添加回第一列中的原始数组。后来我想到了如何对零件号求和并将它们与匹配的零件号一起添加到数组的第二列。
这是我得到的示例:
CPN: QTY:
5551 1
5552 3
5551 1
5551 1
5555 6
这是我需要的输出(排序无所谓)
CPN: QTY:
5551 3
5552 3
5555 6
这是我目前拥有的完整代码。我在 cpns(i, 1) = temp(i)
Sub consolidate()
Dim arrfirst As Integer, arrlast As Integer
Dim cpns() As Variant
Dim CPN_COUNT As Integer
Range("E1000").Select
Selection.End(xlUp).Select
Range(Selection, Selection.End(xlUp)).Select
Range("E2:E4").Select
Range("E4").Activate
CPN_COUNT = Selection.Count
ReDim cpns(1 To CPN_COUNT)
For i = 1 To CPN_COUNT
cpns(i) = Cells(i + 1, 5)
Next
temp = ArrayRemoveDups(cpns)
arrfirst = LBound(temp)
arrlast = UBound(temp)
ReDim cpns(arrfirst To arrlast, arrfirst To arrlast)
For Each i In temp
cpns(i, 1) = temp(i)
Next
End Sub
Function ArrayRemoveDups(MyArray As Variant) As Variant
Dim nFirst As Long, nLast As Long, i As Long
Dim item As String
Dim arrTemp() As String
Dim Coll As New Collection
'Get First and Last Array Positions
nFirst = LBound(MyArray)
nLast = UBound(MyArray)
ReDim arrTemp(nFirst To nLast)
'Convert Array to String
For i = nFirst To nLast
arrTemp(i) = CStr(MyArray(i))
Next i
'Populate Temporary Collection
On Error Resume Next
For i = nFirst To nLast
Coll.Add arrTemp(i), arrTemp(i)
Next i
Err.Clear
On Error GoTo 0
'Resize Array
nLast = Coll.Count + nFirst - 1
ReDim arrTemp(nFirst To nLast)
'Populate Array
For i = nFirst To nLast
arrTemp(i) = Coll(i)
Next i
'Output Array
ArrayRemoveDups = arrTemp
End Function
感谢任何帮助。我确信有一种更简单的方法可以完成所有这些,但我是 VBA
的新手我还应该在我的错误点添加 cpns 的数据类型是 Variant/Variant 并且 temp 的数据类型是 Variant/String.
数组应该使用 For i = Lbound(...) to Ubound(...)
进行迭代,而不是 For Each
。
改变
For Each i In temp
到
For i = Lbound(temp) to Ubound(temp)
正如评论中所指出的,您可以使用 For Each
循环...但是 只是不要。