How do I refer to a controls object, on a worksheet, using a variable name?

后端 未结 4 1912
慢半拍i
慢半拍i 2021-01-11 20:06

I have added a ListBox to a SHEET (not to a \"UserForm\") I did this using the mouse. I clicked the little Hammer and Wrench icon.

This ListBox seems to be easily re

4条回答
  •  执念已碎
    2021-01-11 20:35

    Try this:

    Dim cMyListbox As MSForms.ListBox
    
    Set cMyListbox = Sheet1.ListBox1  '// OR Worksheets("YourSheetName").Listbox1
    
    cMyListbox.AddItem("An option")
    

    Also you can populate a listbox without having to loop through the array, try this:

    Dim cMyListbox As MSForms.ListBox
    Dim vArray As Variant
    
    Set cMyListbox = Sheet1.ListBox1
    
    vArray = Range("A1:A6").Value
    cMyListbox.List = vArray
    

提交回复
热议问题