VBA在弹出窗口中显示我不允许迭代具有用户定义类型的数组。 我写了一些代码,并想知道我如何解决这个问题。 这是一个迷你的例子,专注于我希望能够做的事情。
Option Explicit
Type Info
source As String
destination As String
End Type
Sub specialCopy()
Dim target As Variant
Dim AllTargets() As Info: AllTargets = SetAllTargets()
For Each target In AllTargets
CopyValues (target)
Next
End Sub
Function SetAllTargets() As Info()
Dim A As Info: A = SetInfo("A1", "B1")
Dim B As Info: B = SetInfo("A2", "B2")
Dim AllTargets() As Info
Set AllTargets = Array(A, B)
End Function
Function SetInfo(source As String, target As String) As Info
SetInfo.source = source
SetInfo.destination = destination
End Function
Sub CopyValues(target As Info)
Range(target.source).Select
Selection.Copy
Range(target.destination).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
如何遍历我的AllTargets
数组?
由于我无法编译,因此这里可能存在多个问题。
我不完全确定我设置AllTargets
列表的方式是否有效。
我重复了这个例子来缩小代码中的问题:
Option Explicit
Type Info
source As String
destination As String
End Type
Sub specialCopy()
Dim target As Variant
Dim AllTargets As Collection: Set AllTargets = SetAllTargets()
For Each target In AllTargets
CopyValues (target) '2. unkown if this is possible
Next
End Sub
Function SetAllTargets() As Collection
Dim A As Info: A = SetInfo("A1", "B1")
Dim B As Info: B = SetInfo("A2", "B2")
Set SetAllTargets = New Collection
SetAllTargets.Add (A) '1. problem here when assigning user type
SetAllTargets.Add (B) '1. problem here when assigning user type
End Function
Function SetInfo(source As String, destination As String) As Info
SetInfo.source = source
SetInfo.destination = destination
End Function
Sub CopyValues(target As Info)
Range(target.source).Select
Selection.Copy
Range(target.destination).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
代码从Array转移到Collection - 从来没有更少的问题,我现在无法解决。
我认为根本原因保持不变:使用用户定义的类型。 我标记为评论,我认为问题所在。
答案 0 :(得分:13)
您无法将UDT添加到集合或词典中。我不知道为什么,但它是语言中固有的。您可以创建一个与UDT完全相同的简单自定义类。我再也不会使用UDT了,只是创建一个类来避免这些奇怪的限制。
创建一个新的类模块(Insert - Module)。转到属性表(F4)并将name属性更改为CInfo。
在CInfo类中
Private mSource As String
Private mDestination As String
Public Property Get Source() As String
Source = mSource
End Property
Public Property Let Source(rhs As String)
mSource = rhs
End Property
Public Property Get Destination() As String
Destination = mDestination
End Property
Public Property Let Destination(rhs As String)
mDestination = rhs
End Property
在标准模块中
Sub specialCopy()
Dim target As Variant
Dim AllTargets As Collection: Set AllTargets = SetAllTargets()
For Each target In AllTargets
CopyValues target '2. unkown if this is possible
Next
End Sub
Function SetAllTargets() As Collection
Dim A As CInfo: Set A = SetInfo("A1", "B1")
Dim B As CInfo: Set B = SetInfo("A2", "B2")
Set SetAllTargets = New Collection
SetAllTargets.Add A
SetAllTargets.Add B
End Function
Function SetInfo(Source As String, Destination As String) As CInfo
Set SetInfo = New CInfo
SetInfo.Source = Source
SetInfo.Destination = Destination
End Function
Sub CopyValues(ByRef target As Variant)
Range(target.Source).Select
Selection.Copy
Range(target.Destination).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub