Loop through Listbox and select item if found

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,404
Office Version
  1. 2016
Platform
  1. Windows
I have a multi-select Listbox on a userform and I am trying to select an item using VBA if the value of a textbox is specific.

This is what I have but I keep getting an error that the control source could not be set;

VBA Code:
For i = 1 To 12

If CheckFrm("TextA" & i).Value = "Issues found" Or CheckFrm("TextA" & i).Value = "Not checked" Or CheckFrm("TextA" & i).Value = "Out of service" Then
ListBoxCells.Selected("A" & i) = True
End If

For info, the Listbox is populated when the userform loads like this;

VBA Code:
With ListBoxCells

For i = 1 To 12
.AddItem "A" & i
Next i

For i = 13 To 24
.AddItem "B" & i
Next i

For i = 25 To 36
.AddItem "C" & i
Next i


End With

Can someone please put me out of my misery?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I have found what I thought was a solution but the problem is that it's adding each found item twice to the next Listbox if anyone can assist?

VBA Code:
For x = 0 To ListBoxCells.ListCount - 1
If ListBoxCells.Selected(x) = True Then
With ListBoxCells2
.AddItem ListBoxCells.List(x)
End With
End If
Next x

For x = 0 To ListBoxCells.ListCount - 1
If ListBoxCells.Selected(x) = False Then
With ListBoxCells3
.AddItem ListBoxCells.List(x)
End With
End If
Next x
 
Upvote 0
The selected property of a Listbox control has the appearance (in memory) of an one dimensional array. The members of this array can only be accessed by means of a numeric index value. Since ("A" & i) is by definition alphanumeric due to the letter A, this will result in an error.
 
Upvote 0
From your code alone I cannot determine exactly what you want to achieve.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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