0

While populating the Combo box, i am getting the error

"Run time error 424 Object required".

Below is my code and i am trying to populate the list of countries(range name as countries) in the combo box.

Sub Country()
    Dim Count As Range
    Dim ws As Worksheet

    Set ws = Worksheets("sheet2")

    For Each Count In ws.Range("countries")
        With ComboBox1
            .AddItem Count.Value
        End With
    Next Count
End Sub

Please help me in resolving my errors. Thanks in advance

MatSnow
  • 7,357
  • 3
  • 19
  • 31
ankit
  • 1
  • 1
    On which line is the error? Is this on a userform? – Nathan_Sav Aug 29 '17 at 12:40
  • No ,It is not a useform. The error is coming on the line "With ComboBox1 .AddItem Count.Value" – ankit Aug 29 '17 at 18:28
  • Where is the code? You are just referencing ComboBox1, try referencing it on the sheet that it resides on. Like `With Sheets(1).OLEObjects("ComboBox1").Object` or `Sheets(1).ComboBox1.List = Sheets("countries").Range("A2:A10").Value` – Nathan_Sav Aug 30 '17 at 08:20

2 Answers2

1

Also, from what your code looks like it is doing, why not use the .ListFillRange property to do this rather than a loop.

Dynamically set ListFillRange in Excel ComboBox using VBA

Also where is the code? You are just referencing ComboBox1, try referencing it on the sheet that it resides on. Like

With Sheets(1).OLEObjects("ComboBox1").Object.AddItem 

or

Sheets(1).ComboBox1.List = Sheets("countries").Range("A2:A10").Value
Nathan_Sav
  • 8,466
  • 2
  • 13
  • 20
0

Try changing your variable name from Count to something like nCount

braX
  • 1
  • nothing wrong with using count as a variable name (other than style). It's not a reserved keyword, just a property of many objects. – CallumDA Aug 29 '17 at 13:21