problems deselecting items in a listbox

roscoe

Well-known Member
Joined
Jun 4, 2002
Messages
1,035
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have a list box that allows multiple sections. At some point I want to deselect all the items in the list (code below)

I'm getting a run-time error 1004 on the line with the arrow: "Unable to get the selected property of the Listbox class"

I got this code from online and it looks pretty simple...can't figure out why it's not working. "curlist.ListCount" returns the currect value so I know curlist is being assigned properly...

Code:
Set curlist = sheets("main").Listboxes("List Box 72")
For i = 0 To curlist.ListCount - 1
    If curlist.Selected(i) = True Then  <-------------------
        curlist.Selected(i) = False
    End If
Next

Ideas?

Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
‘For a straight fowared question the answer I developed seems a bit complicated.

‘The problem, I think, is in the way the LB is created, so I created my own.
‘This creation is a one time only, then the rest of the process is the same, except ‘that “Selected” property works

‘Procedure:

‘Copy the code below into a moduel
‘Change the names in the “Const”s to your whims
‘Run CreateListBox (once) (if you need to run it again it will delete the LB first)
‘The rest of the process is the same

‘I have included some testing code as examples

‘-----------------------------------------------
‘-----------------------------------------------
‘-----------------------------------------------
Const LBName = "LBMulti01"
Const LBSheet = "Sheet1"
‘-----------------------------------------------
‘-----------------------------------------------
‘-----------------------------------------------
Sub CreateListBox()
On Error Resume Next
Sheets(LBSheet).ListBoxes(LBName).Delete
On Error GoTo ender
Set LBoxTL = Sheets(LBSheet).ListBoxes.Add(Top:=55, Left:=44, Width:=100, Height:=60)
With LBoxTL
.MultiSelect = xlSimple
.Name = LBName
End With
ender:
End Sub
‘-----------------------------------------------
‘-----------------------------------------------
‘-----------------------------------------------
Sub DeselectAll()
With Sheets(LBSheet).ListBoxes(LBName)
For i = 1 To .ListCount
.Selected(i) = False
Next i
End With
End Sub
‘-----------------------------------------------
‘-----------------------------------------------
‘-----------------------------------------------
Sub SelectedItems()
With Sheets(LBSheet).ListBoxes(LBName)
For i = 1 To .ListCount
If .Selected(i) = True Then
MsgBox .List(i)
End If
Next i
End With
End Sub
‘-----------------------------------------------
‘-----------------------------------------------
‘-----------------------------------------------
 
Upvote 0
I had a whole lot more I tried to add and the site said I waited to long to make the edits and dumped everything. Grrr

My list box is imbedded on the sheet using the Forms control. The input is entered as a named range in the format control box in the input range block.

If that helps...
 
Upvote 0
I'm wondering if the problem is loading the form control listbox from a named range vice using an Active X listbox filled via code.
 
Upvote 0
Seems that was the problem. Simply changing to Active-X took less effort that I thought and solved the problem.

Seems odd that there are two different ways to build a listbox that have the same functionality but require different code to use.
 
Upvote 0

Forum statistics

Threads
1,207,421
Messages
6,078,436
Members
446,337
Latest member
nrijkers

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