VBA: How to reference a ListBox not in a UserForm?

Eyes0ftheworld

New Member
Joined
Apr 23, 2014
Messages
28
Hi All!

I'm having issues referencing a ListBox within a spreadsheet. I'm using Mac Excel 2016 and they unfortunately went away with UserForms all together, so I've been forced to build my own forms within the cells.

Currently, when I want to add a value to ListBox based on a cell value, I use the below code:
Code:
<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008f00; background-color: #ffffff}</style>[COLOR=#008F00][FONT=Menlo]Worksheets("Directions").Shapes("SELECTEDGEOListBox").OLEFormat.Object.AddItem Worksheets("Directions").Range("A103").Value[/FONT][/COLOR]

I feel like there's a simpler way to reference a listbox or button within a spreadsheet. I'm always running into errors when I try and build out macros. When they used to be in a UserForm, it was as simple as just referencing the shapes' name. Any help here would be great. Thanks!
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try:
Code:
Worksheets("Directions").Listboxes("SELECTEDGEOListBox").AddItem Worksheets("Directions").Range("A103").Value
 
Upvote 0
There is. You don't need VBA for what you try to accomplish with your code...

Create a named range f.ex in Sheet2 cell A1, let's call this named range "myID"
Create a Named range with the values you want listed in your ListBox, f.ex Sheet2 A2:A5, call it "myList"
(You don't need named ranges, but I find it easier to work with)

Rightclick your ListBox and select "Format control..."
In "Input range:" insert the name "myList"
In "Cell link:" insert the name "myID"
Then in Sheet1 cell A103, insert the formula:
Code:
=INDEX(myList,myID)

You can make "myList" dynamic by going to the name manager and insert this formula for "myList":
Code:
=OFFSET(Sheet2!$A:$A;1;;COUNTA(Sheet2!$A:$A)-1)

EDIT: You where showing a different code before where you used:
Code:
With Worksheets("Sheet1").Shapes("ListBox").ControlFormat
    Worksheets("Sheet1").Range("A103") = .List(.Value)
End With
Which my answer was based on...
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,552
Messages
6,120,172
Members
448,948
Latest member
spamiki

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