用户在数组/集合中以及每个循环中定义的类型

Joh*_*nes 8 foreach vba types

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
Run Code Online (Sandbox Code Playgroud)

我怎么能遍历我的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
Run Code Online (Sandbox Code Playgroud)

代码从Array转移到Collection - 从来没有更少的问题,我现在无法解决.

我认为根本原因保持不变:使用用户定义的类型.我标记为评论,我认为问题所在.

Dic*_*ika 14

您无法将UDT添加到集合或词典.我不知道为什么,但它是语言中固有的.您可以创建一个与UDT完全相同的简单自定义类.我再也不使用UDT了,只是创建一个类来避免这些奇怪的限制.

创建一个新的类模块(插入 - 模块).转到属性表(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
Run Code Online (Sandbox Code Playgroud)

在标准模块中

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
Run Code Online (Sandbox Code Playgroud)

  • 其他一些语言有一种叫做语法糖的东西。VBA 提供了语法上的苦涩。 (4认同)
  • 好点子。我编辑了答案以包括如何插入类以及如何重命名它。 (2认同)