Excel VBA:如何将类型结构的一个元素传递给vba中的函数

用户名

我在模块中定义了一个结构,如下所示:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将集合中的多个元素传递给一个或多个可变的函数?

Excel VBA将参数传递给OLEDBConnection

如何将参数从Excel / VBA传递到Rstudio中的脚本

excel vba:如何将组合框值粘贴到下一个列单元格

Excel VBA-For Loop需要跳过Web Scrape中的第一个元素

如何创建一个函数来接受串联的源值-Excel VBA

将Excel数组常量传递给VBA函数

如何将两个单元格值的文本合并为一个| EXCEL | VBA |

Excel:定义一个VBA函数以缩短PERCENTRANK

Excel VBA将数组数组传递给函数

将键传递给函数作为字符串参数时,VBA Excel错误“按参考参数类型不匹配”

从传递的列/行/范围VBA Excel中获取一个单元格

在Excel VBA中将变量传递给外接程序函数

在另一个单元格VBA Excel中执行用户定义的函数

如何将动态范围字符串文本传递给EXCEL VBA中的SQL查询IN子句

如何将表名传递给Excel中的函数

Excel VBA宏将多个excel中使用的单元格复制到一个excel中

在 VBA for Excel 中,如何将以下代码限制为仅在一个选项卡中运行?

如何将数据从组合框传递到 Excel VBA 中的命令按钮?

Excel VBA:如何将公式放在唯一行数的下一个可用列中?

如何从一个 Excel 列中删除与另一个 Excel 文件匹配的行?使用 VBA

EXCEL VBA 如何将日期从输入框中传递到天数公式?

如何将参数值从 sub 传递给 excel VBA shell 命令以进行远程执行?

vba excel:如何将字符从一个单元格复制到另一个单元格

在 VBA 函数中引用另一个 Excel 选项卡

如何将活动工作簿保存在 Excel VBA 的另一个文件夹中?

在 Excel VBA 中查找 Object/ArrayList 的最后一个元素

如何编写VBA的Excel函数具有输入作为一个数组?

将 Excel 命名范围存储为 VBA 中的变量,然后传递给函数