vba 数组/集合和每个循环中的用户定义类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24954454/
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 03:58:38  来源:igfitidea点击:

User defined types in arrays/collections and for each loops

vbatypesforeach

提问by Johannes

VBA shows in a popup that i am not allowed to iterate through an array with user defined types. I wrote a bit of code and wonder how i can work around this. Here is a mini example that focusses on what i want to be able to do.

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

How can i iterate through my AllTargetsarray? Since i am unable to compile this there may be more than one problem here. I am not entirely sure if the way i set up the AllTargetslist is a valid syntax.

如何遍历我的AllTargets数组?由于我无法编译这个,这里可能有不止一个问题。我不完全确定我设置AllTargets列表的方式是否是有效的语法。



I reworked the example to narrow down the problems in the code:

我重新编写了示例以缩小代码中的问题:

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

The code went from Array to Collection - never the less there are still issues in it that i can not solve now.

代码从 Array 到 Collection - 无论如何,它仍然存在我现在无法解决的问题。

I think the root cause stayed the same: using user defined types. I marked as a comment where i think the problems are located.

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

回答by Dick Kusleika

You can't add UDTs to collections or dictionaries. I don't know why, but it's inherent in the language. You can make a simple custom class that does the same thing as the UDT. I never use UDTs any more and just create a class to avoid these strange limitations.

您不能将 UDT 添加到集合或字典中。我不知道为什么,但它是语言固有的。您可以创建一个与 UDT 执行相同操作的简单自定义类。我不再使用 UDT,只是创建一个类来避免这些奇怪的限制。

Create a new class module (Insert - Module). Go to the properties sheet (F4) and change the name property to CInfo.

创建一个新的类模块(插入 - 模块)。转到属性表 (F4) 并将 name 属性更改为 CInfo。

In a class 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

In a standard module

在标准模块中

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