Johnny C
Well-known Member
- Joined
- Nov 7, 2006
- Messages
- 1,069
- Office Version
-
- 365
- Platform
-
- 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
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