Excel VBA: Get the First Item in a Dictionary

VBA & macros · Updated November 21, 2026

To get the first key from a VBA Scripting.Dictionary, copy its keys into a Variant array and read the first array element: assign dict.Keys to a variable, then use keys(LBound(keys)). To get the item (the value) associated with that key, use dict(firstKey). Check that the dictionary is not empty before accessing its keys.

What “first item” means in a dictionary

A dictionary stores pairs: each key identifies one associated item, or value. For example, a dictionary might contain the key North and the value 12. In that case, “the first item” could mean the first key, the first value, or the first key-value pair. The code below retrieves all three: it gets the first key, looks up its value, and lets you use either one.

This distinction matters because a dictionary does not work like a worksheet range or a numbered VBA array. You do not normally ask a dictionary for its “item at position 1.” Instead, retrieve its keys as an array, select a key from that array, and use the key to look up the corresponding value. A key must be unique, while values do not have to be.

Get the first key and its value

This complete example creates a late-bound dictionary, adds two entries, and displays the first key and its value. It uses the Windows version of Excel for Microsoft 365.

Sub ShowFirstDictionaryEntry()
    Dim dict As Object
    Dim keys As Variant
    Dim firstKey As Variant
    Dim firstValue As Variant

    ' Create a Scripting.Dictionary on Windows.
    Set dict = CreateObject("Scripting.Dictionary")

    dict.Add "North", 12
    dict.Add "South", 8

    ' A dictionary with no entries has no first key.
    If dict.Count = 0 Then
        MsgBox "The dictionary is empty."
        Exit Sub
    End If

    ' Keys returns an array; use its lower bound for the first key.
    keys = dict.Keys
    firstKey = keys(LBound(keys))
    firstValue = dict(firstKey)

    MsgBox CStr(firstKey) & ": " & CStr(firstValue)
End Sub

With the example entries, the message displays North: 12. The important sequence is keys = dict.Keys, followed by firstKey = keys(LBound(keys)). The first line obtains an array containing the dictionary’s keys. The second reads the first element in that array. Looking up dict(firstKey) then returns the value paired with that key.

Before running any macro, save a copy of the workbook. VBA changes cannot be undone with Ctrl+Z, even if a macro appears to make only a small change. This particular example only displays a message, but the same precaution matters when adapting code to edit cells or files.

Why use LBound instead of assuming zero

The array returned by Keys is commonly treated as zero-based, so examples often use keys(0). Using LBound(keys) makes the intent explicit and asks VBA for the array’s actual lowest index. It also avoids baking an index assumption into code that may later be adapted to work with a different array.

Keep keys declared as a Variant. The dictionary’s Keys method returns an array, and assigning that result to a Variant is the usual way to hold it. Do not declare keys as a simple String if you need to keep the whole array. After assignment, the array is a snapshot of the keys at that point in the procedure. If the dictionary changes later, retrieve dict.Keys again to get an updated list.

Does the first key mean the first one added?

Not necessarily in a way your program should depend on. A dictionary is designed for looking up values by key, not for providing a documented sorting or sequencing rule. In common Windows use, enumerating a Scripting.Dictionary often appears to follow the order in which entries were added, but you should not treat that behavior as a guaranteed ordering contract for an application that requires a specific sequence.

If your requirement is “the first key I added,” and that sequence is important, keep a separate ordered list of keys as you add entries. If the requirement is “the alphabetically smallest key,” copy the keys to an array and sort that array before selecting one. If the requirement is simply “any key,” retrieving the first element of the keys array is usually adequate. Being precise about the meaning of first prevents a subtle bug when code is moved, modified, or fed different data.

Also distinguish first from last. The last array element can be read using keys(UBound(keys)), but that does not make it a reliably meaningful “last added” key unless your code has deliberately maintained and verified the ordering you need.

Handle an empty dictionary safely

When dict.Count is zero, there is no key to retrieve. Attempting to read a key from an empty result can raise a runtime error. Test the count before requesting the first element, as the example does. This is especially important when a dictionary is populated from a worksheet, file, or user input, because the source may contain no usable records.

For reusable code, decide what an empty dictionary should mean in your application. You might show a message, skip the operation, return a status flag, or raise a controlled error. Avoid quietly returning an unrelated default key or value: that can make a later calculation look valid while actually using missing data. If a function returns a key, its interface should also make clear how it signals that no key exists.

Creating the dictionary and setting a reference

The example uses CreateObject("Scripting.Dictionary"), known as late binding. This avoids requiring a project reference at compile time and is convenient for a small Windows macro. For a project using early binding, open the Visual Basic Editor, choose Tools > References, and select the Microsoft Scripting Runtime if it is available. Then declare the object as Scripting.Dictionary and create it with New Scripting.Dictionary. Early binding provides the dictionary’s type information while editing, but a workbook shared with computers that lack the same reference can encounter a missing-reference problem.

On Windows, open the VBA editor with Alt+F11, insert a standard module using Insert > Module, and place the procedure there. On Mac, the VBA editor shortcut is Option+F11 (some keyboards require Fn as well). The Windows Scripting.Dictionary COM object is not a dependable cross-platform choice for Mac Excel; availability can differ, so do not assume that a workbook using CreateObject("Scripting.Dictionary") will run unchanged on every Mac. If Mac support is required, use a platform-compatible approach such as a VBA Collection or maintain an array/list alongside the data, and test on the target Excel versions.

Macros need to be saved in a macro-enabled workbook, such as an .xlsm file, if you want to retain the VBA code. Only enable macros in workbooks you trust. The Windows and Mac VBA editors differ in some details, but the array logic for selecting a key is ordinary VBA; the main compatibility concern here is creating the dictionary object.

Alternatives when order matters or Dictionary is unavailable

A VBA Collection is built into VBA and supports ordered entries, but it does not offer exactly the same key-handling behavior as Scripting.Dictionary. You can add items with keys and iterate through a Collection, but retrieving the first entry by position and managing duplicate keys work differently. Choose it when its behavior matches the task rather than treating it as a drop-in replacement.

For data that already lives in a worksheet, a range or a table may be the simplest ordered source. Read the first data row directly if “first” means topmost row, or sort the table explicitly if “first” means a particular alphabetical or date order. For a custom dictionary-like structure, keep a separate array of keys in the required order and use the dictionary only for fast key-based lookups. This separates ordering from lookup instead of relying on incidental enumeration behavior.

Sorting keys is another option, but it changes the meaning of first: it becomes the first according to the chosen sort rule, not the first inserted. For text keys, decide whether case should matter and what locale or comparison rules are appropriate. If the keys represent dates or numbers, store and sort values in a type-aware way rather than assuming text order is numeric or chronological.

Common mistakes and how to fix them

  • Using dict(0): this asks for the value associated with the key 0; it does not mean the value at position zero. Retrieve a key from dict.Keys first.
  • Calling dict.Keys(0) directly: VBA syntax can be awkward when indexing a method’s returned array. Assign the result to a Variant variable, then index that variable.
  • Forgetting the empty check: test dict.Count before reading an element so an empty source does not cause a runtime error.
  • Expecting the value rather than the key: keys(LBound(keys)) is the key. Retrieve its paired value with dict(firstKey).
  • Assuming a fixed order: if a particular order is a requirement, store or impose that order explicitly rather than relying on dictionary enumeration.
  • Getting an automation error on Mac: the Windows Scripting Runtime object may not be available in the Mac environment. Use a compatible data structure or a Mac-tested implementation.

Frequently Asked Questions

How do I get the first value in a VBA dictionary?
Get the first key from dict.Keys, then use that key to retrieve the value: firstValue = dict(firstKey). Check that dict.Count is greater than zero first.

Can I use dict.Keys()(0) in VBA?
Some VBA contexts and examples show direct indexing, but assigning the result of dict.Keys to a Variant and then indexing the variable is clearer and avoids syntax trouble. Use keys(LBound(keys)) for the first array element.

Does a VBA dictionary keep keys in insertion order?
Do not rely on insertion order as a guaranteed rule for code that needs a specific sequence. Keep a separate ordered key list or sort the keys when order is part of the requirement.

What happens if the dictionary is empty?
There is no first key. Check dict.Count before reading the keys array, then handle the empty case explicitly to avoid a runtime error.

Does Scripting.Dictionary work in Excel for Mac?
Scripting.Dictionary is a Windows COM object and is not a dependable cross-platform choice. For Mac-compatible workbooks, consider a Collection or another data structure and test it in the Excel environment where the macro will run.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.