Listbox selected state initialisation problem

Johnny C

Well-known Member
Joined
Nov 7, 2006
Messages
1,069
Office Version
  1. 365
Platform
  1. Windows
I'm setting up a macro to strip names out of sheets (some of the sheets have hundred of names which point to defunct spreadhseets).

I've set up a 2 column listbox with multiselection which populates okay. When I added a line to automatically select one of the rows if the name text had a '#REF' in, it crashes with error 380 - Could not set the selected property; invalid property value

The line it's bombing out on is
If InStr(1, n, "#REF", vbTextCompare) Then ListBox1.Selected(tempnum) = True

The instr works okay, but even on it's own the "ListBox1.Selected(tempnum) = True" line causes the error. According to my VBA bible it should have a boolean value (false by default) and you can set it.

When I wrote the line and closed the parenthesis it offered the ' = True' as one of two options (the other being false of course).

Can anyone give me a clue what I've done wrong please?

Code is:-

Private Sub UserForm_Initialize()

With RedundantNamesForm.ListBox1
.RowSource = ""
.ColumnWidths = "150;"
End With

Dim NamedRanges()

Dim NameCounter%
NameCounter = 0
For Each n In ActiveWorkbook.Names
NameCounter = NameCounter + 1
Next

ReDim NamedRanges(1 To NameCounter, 1 To 2)

NameCounter = 0
For Each n In ActiveWorkbook.Names
NameCounter = NameCounter + 1
NamedRanges(NameCounter, 1) = n.Name
NamedRanges(NameCounter, 2) = n
If InStr(1, n, "#REF", vbTextCompare) Then ListBox1.Selected(NameCounter) = True
Next n

ListBox1.List = NamedRanges

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Johnny

Is the listbox populated when you run this code?

You appear to be populating it at the end here.
Code:
ListBox1.List = NamedRanges
So when you run the previous code there's nothing in the listbox to be selected.

Unless I'm missing something.:eek:
 
Upvote 0
Dozy t**t, the listbox.list wasn't set up :)
 
Upvote 0
Okay, got it working now. I've also got some buttons to select anything with a #Ref in and also to select external links (for where there are conflicting names).

I am having trouble accessing the second column in the list; I tried using BoundColumn=2 and rebuilding the list but it still only gives me the range name, not the range it points to.

Is there a way I can access the second column data directly? At the mo I'm building up the array again each time (not really a problem as no-one else can access the spreadsheet whilst I've got it open) but it's a bit of a crude way to do it, if I could get the range sting from the listbox directly it would make it far simpler.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,739
Members
449,050
Latest member
excelknuckles

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