OLEObject ComboBox or ListBox

Flue

Board Regular
Joined
Feb 5, 2009
Messages
106
Anyone know the reason why when I populate a ComboBox or a ListBox via VBA the values clear out as soon as I click anywhere else on the sheet, or save and reopen?

Does the box try to revert the linked cell that has not been defined after code has finished running?

TIA.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
How are you populating the combobox/listbox?
 
Upvote 0
Here is the code I have working.

Code:
Dim Desc as String
Dim lstDup As Boolean
Dim WS As Excel.Worksheet

  Application.ScreenUpdating = False

Windows("Master.xls").Activate
Sheets("Master_MAR11").Select
  Set WS = ActiveSheet
  
  'create checkbox
  Set ShtComboBx = WS.OLEObjects.Add("Forms.ComboBox.1")
  With ShtComboBx
    .Left = WS.Range("G2").Left
    .Top = WS.Range("G2").Top
    .Name = "AnsiCombo"
    .Width = "400"
    .Height = "24.00"
  End With

Windows("Master.xls").Activate

WS.OLEObjects("AnsiCombo").Object.Clear

myRow = Application.CountA(ActiveSheet.Range("A:A"))
For j = 3 To myRow
lstDup = False
What = Trim$(Cells(j, 1))

'Search for Code
Windows("CodeList.xls").Activate
   
Do
        Set rng = ActiveSheet.Range("A:A").Find(What, lookat:=xlWhole)
        If rng Is Nothing Then
                        Exit Do
        Else
            Desc = Cells(rng.Row, 2)
            Exit Do
        End If
    Loop
Windows("Master.xls").Activate
For i = 0 To WS.OLEObjects("AnsiCombo").Object.ListCount - 1
    If WS.OLEObjects("AnsiCombo").Object.List(i) = What & " - " & Desc Then
    lstDup = True
    Exit For
    End If
Next

If lstDup = False And What <> "" Then
  WS.OLEObjects("AnsiCombo").Object.AddItem What & " - " & Desc
End If

Next

Which ever item is displayed when the worksheet is saved is the only entry available in the combobox when the worksheet is reopened. Listbox is blank when reopened.

Thanks.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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