VBA on Mac issue

Santek Brenner 80 Reputation points
2025-12-17T10:07:47.1133333+00:00

I’m trying to automate a simple task in Excel using VBA on macOS, but the macro doesn’t behave the same way as it does on Windows. Specifically, code that relies on Windows-only objects (such as Scripting.Dictionary or file system calls) either throws errors or silently fails on Mac. I’m looking for the correct way to write cross-platform VBA that works reliably on Excel for macOS.

Im using:

Sub CountItems()

Dim dict As Object

Set dict = CreateObject("Scripting.Dictionary")

dict.Add "Apple", 1

dict.Add "Orange", 2

MsgBox "Total items: " & dict.Count
```End Sub

Issue on macOS:

CreateObject("Scripting.Dictionary") fails because the Windows Scripting Runtime is not available on Excel for Mac.  
  
---  
Mod moved from :  Microsoft 365 and Office | Excel | For business | MacOS
Microsoft 365 and Office | Excel | For business | MacOS
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Gabriel-N 10,300 Reputation points Microsoft External Staff Moderator
    2025-12-17T11:07:52.6466667+00:00

    Dear Santek Brenner

    Thank you for posting your question in the Microsoft Q&A forum.

    To make your VBA code work cross‑platform (Mac & Windows) without complex workarounds, you can simply replace the Scripting.Dictionary with the native VBA Collection object.

    Please test the following code and let me know if it meets your requirements:

    Sub CountItems()
        ' Use "New Collection" instead of CreateObject to work on Mac
        Dim coll As New Collection
        ' IMPORTANT: Collection.Add syntax is reversed compared to Dictionary
        ' Dictionary: .Add Key, Value
        ' Collection: .Add Value, Key
        coll.Add 1, "Apple"
        coll.Add 2, "Orange"
        MsgBox "Total items: " & coll.Count
    End Sub
    
    

    If this solution doesn’t fully address your needs, please share your feedback so I can assist further.


    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.