gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:49:48 25 4
gpt4 key购买 nike

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 的方式是否正确。 list 是一个有效的语法。

我修改了示例以缩小代码中的问题:
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 - 仍然存在我现在无法解决的问题。

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

最佳答案

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

创建一个新的类模块(插入 - 模块)。转到属性表 (F4) 并将名称属性更改为 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

关于vba - 数组/集合和每个循环中的用户定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24954454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com