如何在Power Point应用程序中查找字典中的最大值?

问题描述 投票:0回答:3

以下代码在 Excel 中可以正常工作。

但我正在寻找 Power Point 解决方案。

如何在 Power Point 应用程序中查找字典中的最大值?

    Sub Macro1()
    
        Dim dict As Object
        Set dict = CreateObject("Scripting.Dictionary")
        
        For i = 1 To 10
            dict.Add Key:=i, Item:=i
        Next i
        
        For i = 0 To dict.Count - 1
           Debug.Print dict.Keys()(i), dict.Items()(i)
        Next i
        
        'The following two lines works properly in Excel. 
        'But I am looking for Power Point solution.
        Debug.Print Application.Max(dict.Items)
        Debug.Print WorksheetFunction.Max(dict.Items)
    
    End Sub
vba dictionary max powerpoint
3个回答
1
投票

这适用于 Powerpoint:

Sub Macro1()
    
        Dim dict As Object
        Set dict = CreateObject("Scripting.Dictionary")
        
        Dim i As Long
        For i = 1 To 10
            dict.Add Key:=i, Item:=i
        Next i
        
        Dim iMax As Long
        For i = 0 To dict.Count - 1
           Debug.Print dict.Keys()(i), dict.Items()(i)
           If dict.Items()(i) > iMax Then iMax = dict.Items()(i)
        Next i
        
        Debug.Print iMax
    End Sub

您必须检查循环内的最大值。


1
投票

在 Excel-VBA 中,

WorksheetFunction
是访问 Excel 提供的函数(而非 VBA)的一种方式。 Powerpoint里没有这个功能。

因此,最好的选择是循环遍历字典中的所有条目:

Dim key As Variant, maxValueKey As Variant, maxValue As Variant
For Each key In dict.keys
    If IsEmpty(maxValue) Or dict(key) > maxValue Then
        maxValue = dict(key)
        maxValueKey = key
    End If
Next key
Debug.Print "The max value found = " & maxValue & " at key " & maxValueKey

1
投票
  • 用UDF捕获最大值
Sub Macro1()
    
    Dim dict As Object, i As Long
    Set dict = CreateObject("Scripting.Dictionary")
    
    For i = 1 To 10
        dict.Add Key:=i, Item:=i
    Next i
    
    For i = 0 To dict.Count - 1
        Debug.Print dict.Keys()(i), dict.Items()(i)
    Next i
    
    Debug.Print GetMax(dict.Items())
    
End Sub

Function GetMax(arr) As Long
    Dim maxVal As Long, i As Long
    If IsArray(arr) Then
        maxVal = -1000000000#
        For i = LBound(arr) To UBound(arr)
            If maxVal < arr(i) Then maxVal = arr(i)
        Next
        GetMax = maxVal
    Else
        GetMax = arr
    End If
End Function

© www.soinside.com 2019 - 2024. All rights reserved.