Search code examples
vbavariant

VBA: how to null a Variant?


I have the following Variant:

Dim comboitems() As Variant

that I use in all the code as an array that contains the values of a ComboBox control.

In a certain point of the code, I need to clear/empty/null comboitems(). How can I do? I tried all of the following options without any success.

comboitems = ""
comboitems = Null
comboitems = Nothing
Set comboitems = ""
Set comboitems = Null
Set comboitems = Nothing
comboitems() = ""
comboitems() = Null
comboitems() = Nothing
Set comboitems() = ""
Set comboitems() = Null
Set comboitems() = Nothing

The error that I get is this:

enter image description here


Solution

  • For variant arrays, you'd clear them with the erase command.

    Erase comboitems
    

    Here's a handy reference guide for dealing with arrays in vba:

    https://excelmacromastery.com/excel-vba-array/