Help troubleshooting ListBox additem

AnswersAreEasy

New Member
Joined
Aug 18, 2016
Messages
6
Hi, I've only been working with VBA for three hours so be a little patient.

I have a listbox on my worksheet "Main", and I want the options on this to update from a variable list (N3 - N*).
This is what I have right now for the code:
Private Sub ListBox10_Change()
Dim SeamRa As Object
Set SeamRa = Range("N3")
If Len(SeamRa.Formula) = 0 Then
Call SeamSort
Else
If Len(SeamRa.Formula) > 0 Then
.ListBox10.AddItem.SeamRa
SeamRa = SeamRa.Offset(1, 0)
End If
End If
End Sub




I'm getting an error that the Sub line is "Invalid or unqualified",
where I introduce the SeamRa object it says as an error "object rRange is undefined", even though I have no rRange object. And finally, where I add the SeamRa item to my list box, it says ListBox10 is undefined.
Additionally, this code is located on Module1. The listbox and range source are on the same sheet, but eventually I want to move the range source over to a different worksheet.

So far my coding has been going pretty well but I am completely stumped on this one.
Thanks you guys!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Of course the moment I post it I realize a flaw in the code. This is the updated code with a loop:

Private Sub ListBox10_Change()
Dim SeamRa As Object
Set SeamRa = Range("N3")
If Len(SeamRa.Formula) = 0 Then
Call SeamSort
Else
Do Until Len(SeamRa.Formula) = 0
.ListBox10.AddItem.SeamRa
SeamRa = SeamRa.Offset(1, 0)
Loop
End If
End If
End Sub


I'm still getting the same errors though....
 
Upvote 0
It should just be this.
Code:
.ListBox10.AddItem  SeamRa
 
Upvote 0
You need a worksheet reference for the listbox.
 
Last edited:
Upvote 0
Entered the line as this, with the worksheet reference
Code:
Worksheets("Main").Shapes("ListBox10").AddItem SeamRa

or as just
Code:
Worksheets("Main").ListBox10.AddItem SeamRa

and still getting the same reference error for the ListBox10 object. I know I'm using the correct listbox name too.
 
Upvote 0
How did you create the listbox?
 
Upvote 0
Inserted it as a Listbox form into an excel cell.

I've changed up the code a bit now, now I call a sub PopulateList from another sub. I do this with:
Code:
   Worksheets("Main").Shapes("ListBox10").OnAction = "PopulateList"

then the code runs, which I have now edited to be:
Code:
Sub PopulateList()
Dim SeamRa As Object
Set SeamRa = Range("N3")
If Len(SeamRa.Formula) = 0 Then
Call SeamSort
Else
Do Until Len(SeamRa.Formula) = 0
Worksheets("Main").Shapes("ListBox10").AddItem SeamRa
SeamRa = SeamRa.Offset(1, 0)
Loop
End If
End Sub
[/sub]
Sorry for changing it up so much here, I'm just trying to find an answer through google too.
 
Upvote 0
Did you create it from the ActiveX or the Forms toolbar?
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

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