是否有使用VBA将集合直接写入工作表?
之类的东西Worksheets("Sheet3").Range("C2") = MyCollection
无法找到有效的内容
答案 0 :(得分:4)
已编辑为" Class"添加代码方法,而不是" Sub"一个已经发布的
对Jake的回答最真实的解决方案是Range
类的扩展方法(可能名为" WriteCollection")。
不幸的是,VBA并不允许这样的(以及许多其他的)功能,但可以通过类模块方法在某种程度上模仿它
<强>&#34;级&#34;方法强>
插入课程模块
通过单击&#34;插入 - &gt;类模块&#34>将类模块插入到项目中从主菜单
你会看到一个&#34; Class1&#34;子节点出现在&#34;类模块&#34; &#34;项目经理&#34;中的节点窗口
重命名
将其起始名称更改为有意义的名称
您可以通过单击&#34;项目管理器&#34;中的类子节点来完成此操作。窗口和编辑&#34;(名称)&#34; &#34;属性&#34;中的属性值窗口(如果你的VBE中没有这个窗口,请按F4)
让我们将其重命名为&#34; MyRange&#34;,但您可以选择在后续步骤中提供的与其一致的名称
输入类代码
进入类代码窗格(双击&#34;项目管理器&#34;窗口中的子节点类)并添加以下代码:
`
Option Explicit
'---------------------------------------------------------------------
'Properties
'---------------------------------------------------------------------
' all 'Public' properties will be shown by Intellisense for every variable declared of this class
Public Range As Range '<~~ declare a property of the "Range" class. it therefore will have all methods and property of a "Range" object
'---------------------------------------------------------------------
'---------------------------------------------------------------------
'Methods
'---------------------------------------------------------------------
' all 'Public' methods will be shown by Intellisense for every variable declared of this class
Public Sub WriteCollection(myColl As Collection, Optional direction As Variant) '<~~ this will be the method to call and have a Collection written down into a range
If IsMissing(direction) Then direction = "V"
If direction = "V" Then
Range.Resize(myColl.Count).Value = CollToRangeVertical(myColl)
Else
Range.Resize(, myColl.Count).Value = CollToRangeHorizontal(myColl)
End If
End Sub
'---------------------------------------------------------------------
'---------------------------------------------------------------------
'helpers
'---------------------------------------------------------------------
' these are subs/function needed for the inner functioning of the class, so they are declared as 'Private' to prevent their use from outside it
Private Function CollToRangeVertical(myColl As Collection) As Variant
CollToRangeVertical = Application.Transpose(CollToStrng(myColl))
End Function
Private Function CollToRangeHorizontal(myColl As Collection) As Variant
CollToRangeHorizontal = CollToStrng(myColl)
End Function
Private Function CollToStrng(myColl As Collection) As Variant
Dim i As Long
For i = 1 To myColl.Count
CollToStrng = CollToStrng & CStr(myColl(i)) & "|"
Next i
If CollToStrng <> "" Then CollToStrng = Split(Left(CollToStrng, Len(CollToStrng) - 1), "|")
End Function
'---------------------------------------------------------------------
'---------------------------------------------------------------------
利用您的课程
将任何模块添加到您的类所在的同一个项目中,并输入以下示例
`
Option Explicit
Sub main()
Dim MyCollection As New Collection
Dim mRange As New MyRange '<~~ declare a variable of the MyRange class and set a new instance of it
' fill Collection
MyCollection.Add "C"
MyCollection.Add "B"
MyCollection.Add "A"
'set the "Range" property of your "mRange" object of the "MyRange" class
Set mRange.Range = Worksheets("Sheet3").Range("C2") '<~~ it can be set to any range in any open workbook and worksheet
'exploit "MyRange" class method(s)
mRange.WriteCollection MyCollection '<~~ write down a collection "vertically"
mRange.WriteCollection MyCollection, "H" '<~~ write down a collection "horizontally"
'exploit Excel "Range" class methods through the "Range" property of "MyRange" class object
With mRange.Range
.Offset(10, 1).Value = .Value & "- offsetted" '<~~ copy/paste values
.Resize(.Rows.Count, 1).Sort Key1:=.Range("A1") '<~~ sort the collection you just wrote down
End With
End Sub
<强>&#34;子&#34;方法强>
你是否需要继续收集
不,没有这样的直接&#34;方法&#34;对于Range
对象
无论如何,他们的呼唤应该比你想要的更明确,因为你必须至少指定哪个&#34;方向&#34;从最初的单元格扩展集合项
但它也应该处理每个集合项的实际类型
只是为了练习我放下了一个&#34; WriteColl&#34; Sub带有以下&#34;签名&#34;:
WriteColl, Range, Collection [, direction]
其中direction是一个可选参数,用于指定从给定范围扩展粘贴集合项的方向
因此可以使用如下:
WriteColl Worksheets("Sheet3").Range("C2"), MyCollection
要执行此操作,您必须在任何模块中包含以下代码:
Option Explicit
Sub main()
Dim MyCollection As New Collection
MyCollection.Add "A"
MyCollection.Add "B"
MyCollection.Add "C"
WriteColl Worksheets("Sheet3").Range("C2"), MyCollection
WriteColl Worksheets("Sheet3").Range("D2"), MyCollection, "H"
End Sub
'method
Sub WriteColl(myRng As Range, myColl As Collection, Optional direction As Variant)
If IsMissing(direction) Then direction = "V"
If direction = "V" Then
myRng.Resize(myColl.Count).Value = CollToRangeVertical(myColl)
Else
myRng.Resize(, myColl.Count).Value = CollToRangeHorizontal(myColl)
End If
End Sub
'helpers
Function CollToRangeVertical(myColl As Collection) As Variant
CollToRangeVertical = Application.Transpose(CollToStrng(myColl))
End Function
Function CollToRangeHorizontal(myColl As Collection) As Variant
CollToRangeHorizontal = CollToStrng(myColl)
End Function
Function CollToStrng(myColl As Collection) As Variant
Dim i As Long
For i = 1 To myColl.Count
CollToStrng = CollToStrng & CStr(myColl(i)) & "|"
Next i
If CollToStrng <> "" Then CollToStrng = Split(Left(CollToStrng, Len(CollToStrng) - 1), "|")
End Function