vba:从函数返回字典

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

vba: return dictionary from function

excelvbaexcel-vbaexcel-2003

提问by jason m

this outlines what i am trying to do.

这概述了我正在尝试做的事情。

this is not working for me, and it is unclear why.

这对我不起作用,目前尚不清楚原因。

thank you in advance for any and all help.

提前感谢您的任何帮助。

        Sub mySub()
        dim myDict as Dictionary
            myDict=new Dictionary

                myDict=myFunc()

        End Sub

        Function myFunc()
            dim myDict2
                set myDict2 = new Dictionary

                    'some code that does things and adds to myDict2'

            myFunc=myDict2
        End Function

回答by BradC

You'll need to use the SET keyword anytime you are assigning an objectinstead of a value:

您需要在任何时候分配对象而不是值时使用 SET 关键字:

    Sub mySub()
        dim myDict as Dictionary
        set myDict = myFunc()
    End Sub

    Function myFunc() as Dictionary
        dim myDict2 as Dictionary
        set myDict2 = new Dictionary
                'some code that does things and adds to myDict2'
        set myFunc=myDict2
    End Function

Your original code was also creating myDict as a new Dictionary object, then immediately replacing it with a different one. You can just skip that step.

您的原始代码还将 myDict 创建为一个新的 Dictionary 对象,然后立即将其替换为不同的对象。你可以跳过这一步。

回答by HopWorks

I see that this is an old question, but the post AND solution helped me figure this out, and I took it to the next level. It was a small leap. Thank you!

我看到这是一个老问题,但帖子和解决方案帮助我解决了这个问题,我把它提升到了一个新的水平。这是一个小小的飞跃。谢谢!

How about converting your function that populates the dictionary with process names and IDs so it returns a dictionary object? Then it is a simple task of populating a sheet with the dictionary contents, which I learned to do from a blog. I wish I had the author's name but the link is included.

如何转换用进程名称和 ID 填充字典的函数,以便它返回一个字典对象?然后是用字典内容填充工作表的简单任务,这是我从博客中学到的。我希望我有作者的名字,但链接已包含在内。

Sheet1 was assumed of course. Customize however you wish. Again this was a small leap from what both of you posted. Absolutely brilliant work guys, thank you!

当然假设 Sheet1。随心所欲地定制。这与你们两人发布的内容相比又是一个小小的飞跃。绝对出色的工作人员,谢谢!

Sub Test_AllRunningApps()
    Dim apps As Dictionary
    Set apps = AllRunningApps()

    'Populate a sheet with a dictionary - http://exceldevelopmentplatform.blogspot.com/2018/05/vba-writing-dictionaries-to-worksheet.html
    Sheet1.Cells(1, 1).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Keys)
    Sheet1.Cells(1, 2).Resize(apps.Count, 1).Value2 = Application.Transpose(apps.Items)

    Set apps = Nothing
End Sub

'Similar to: http://msdn.microsoft.com/en-us/library/aa393618%28VS.85%29.aspx
Public Function AllRunningApps() As Dictionary
    Dim strComputer As String
    Dim objServices As Object, objProcessSet As Object, Process As Object
    Dim oDic As Object, oDic2 As Object, a() As Variant

    Set oDic = CreateObject("Scripting.Dictionary")

    strComputer = "."

    Set objServices = GetObject("winmgmts:\" _
        & strComputer & "\root\CIMV2")
    Set objProcessSet = objServices.ExecQuery _
        ("Select Name, ProcessID FROM Win32_Process", , 48)

    For Each Process In objProcessSet
       If Not oDic.exists(Process.Name) Then
        oDic.Add Key:=Process.Properties_("Name").Value, Item:=Process.Properties_("ProcessID").Value
       End If
    Next

    Set AllRunningApps = oDic

    Set objProcessSet = Nothing
    Set oDic = Nothing
End Function