VBA invalid argument error on simple combobox

Flammy

Board Regular
Joined
Apr 19, 2011
Messages
51
Not sure what is causing it, thanks.

Code:
Private Sub UserForm_Initialize()
Dim Arrayb(2) As Variant

Arrayb(1) = "test1"
Arrayb(2) = test2
For i = 1 To 2
With ComboBox1
    .AddItem Arrayb(i), i
End With
Next i

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Not sure what is causing it, thanks.

Code:
Private Sub UserForm_Initialize()
Dim Arrayb(2) As Variant

Arrayb(1) = "test1"
Arrayb(2) = test2
For i = 1 To 2
With ComboBox1
    .AddItem Arrayb(i)[SIZE=3][B][COLOR=#B22222], i[/COLOR][/B][/SIZE]
End With
Next i

End Sub
Remove the ", i" (shown in red) and then try it.
 
Upvote 0
Also, unless you've used Option Base 1 to override the default base array subscript of 0, your array will consist of 3 elements. The first element will be empty, the second will be "test1", and the third will be "test2". If you haven't overriden the default value, try replacing...

Code:
Dim Arrayb(2) As Variant

with

Code:
Dim Arrayb(1 to 2) As Variant
 
Upvote 0
Remove the ", i" (shown in red) and then try it.

That solved it. I thought that would control the order they were in in the combobox? it is called "pvargIndex" so that is why I thought it would be order.

Re:Domenic
I already had option base 1 on, thanks though. I was curious and tried it after commenting it out and it still worked fine (well, still failed in the same way). That probably wouldn't cause the error as long as i'm not calling Lbound(array) or anything that used the first one.
 
Upvote 0
That solved it. I thought that would control the order they were in in the combobox? it is called "pvargIndex" so that is why I thought it would be order.
If you don't specify an index, the next item is placed automatically into the next unfilled line. The index argument is good when you want to address a specific item in the ComboBox (to edit it, for example).

Re:Domenic
I already had option base 1 on, thanks though. I was curious and tried it after commenting it out and it still worked fine (well, still failed in the same way). That probably wouldn't cause the error as long as i'm not calling Lbound(array) or anything that used the first one.
I didn't test it, but the problem may result from the fact that the first indexed item is 0, not 1. I am guessing that when you tried to put something in line #1 when there was nothing in line #0, VB balked.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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