我在模块中定义了一个结构,如下所示:
Type Assets
Tags() As String
Names() As String
End Type
Public Assets() As Assets
Assets(x).Tags(0..n)
包含许多文本项以组成位置字符串。Assets(x).Names(0..n)
拥有这些标签的相应“可读”名称。该数组Assets(x)
是从工作表填充的,因此电子表格中的每一行都有一个唯一的x值。Tags()
数据中可能有重复项,因此我需要将列表传递给函数以删除重复项。问题就在这里。我怎样才能传递Assets(0..n).Tags(y)
给一个函数,使其变为类似以下内容:
Public Function RemoveDuplicates(ByRef InputList() As String, ByRef OutputList() As String) As Integer
@ user3598756已经回答了这个问题,但只是出于兴趣:将我的电子表格读入Assets()
数组,我首先从
Assets(1).Tags(1)="PRESS"
Assets(1).Tags(2)="DR1"
Assets(1).Tags(3)="HP1"
Assets(2).Tags(1)="PRESS"
Assets(2).Tags(2)="DR1"
Assets(2).Tags(3)="HP2"
Assets(3).Tags(1)="OUTP"
Assets(3).Tags(2)="DR1"
Assets(3).Tags(3)="SV1"
除其他事项外,我需要使用加载一个列表框Assets(x).Tags(1)
,以便给我: PRESS OUTP
供用户选择。基于此选择,然后我继续到Assets(x).Tag(2)
etc,这样我的列表(myTags
在@ user3598756的代码中)逐渐减少为唯一的选择。
只是从阿克塞尔·里希特(Axel Richter)的答案中猜出来
您可以具有以下功能:
Public Function RemoveDuplicates(myAssets() As Assets, yTag As Long, OutputList As Variant) As Integer
Dim iAsset As Long
With CreateObject("Scripting.Dictionary")
For iAsset = LBound(myAssets) To UBound(myAssets)
.Item(myAssets(iAsset).Tags(yTag)) = .Item(myAssets(iAsset).Tags(yTag))
Next
OutputList = .keys
RemoveDuplicates = .Count
End With
End Function
您可以从“主”代码中调用该代码,如下所示:
Sub main()
Dim Assets() As Assets '<--| declare 'Asset' as a Sub scoped variable and then pass it to other subs as an argument
Dim myTags As Variant '<--| this will be passed to 'RemoveDuplicates()' function to be "transformed" into the the array (a 'Variant' can be that) holding unique tags for the specified tag index
Dim myInt As Integer '<--| this will store the returned integer value from 'RemoveDuplicates()' function
'example of filling Assets
ReDim Assets(0 To 2) As Assets
With Assets(0)
ReDim .Tags(0 To 1)
.Tags(0) = "tag1"
.Tags(1) = "tag2"
End With
With Assets(1)
ReDim .Tags(0 To 1)
.Tags(0) = "tag2"
.Tags(1) = "tag3"
End With
With Assets(2)
ReDim .Tags(0 To 1)
.Tags(0) = "tag1"
.Tags(1) = "tag2"
End With
myInt = RemoveDuplicates(Assets, 1, myTags) '<--| have 'myTags' returned as an array with unique tags out of 'Assets(0...n).Tags(1)': namely it'll return a two elements array containing "tag1" and "tag2". Thus myInt will be 2
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句