trying to create Function that sorts dictionary values then returns a sorted Dictionary

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
833
Office Version
  1. 365
Platform
  1. Windows
Mr. Excl Lister Rijnsent inspired me to look into dictionaries. I'm part way to use of one the way I need...

I successfuly filled (and tested) a dictionary with numerous label/value pairs. I want to sort the dictionary.

I found a vb function here Sort a dictionary by value to do the sort but, apparently, I was unable to hack it. I declare it thusly

VBA Code:
Private Function SortDictionaryDesc(ByVal dict As Dictionary) As Dictionary

    Dim final As New Dictionary

    Dim max As Double
    Dim prev As Double

    Dim key As Variant

    Do While dict.Keys.Count - 1 > 0

        For Each x In dict.Keys
            If dict(x) > max Then
                max = dict(x)
                key = x
            End If
        Next

        final.Add(key) = max
        dict.Remove (key)
        max = 0

    Loop

    SortDictionaryDesc = final

End Function

In main function is a dictionary named dicTopHoldings that is filled with those label/value pairs. I want to sort so I call my function like this

dicTopHoldings = SortDictionaryDesc(dicTopHoldings)

I get compile error Argument not optional.

Heheh, for me, argument is to be avoided!

What basic concept am I missing?

TIA a lot.

Jim
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
A really dumb mistake. Must Set dictionary = ...
 
Upvote 0
Solution

Forum statistics

Threads
1,214,636
Messages
6,120,669
Members
448,977
Latest member
moonlight6

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top