List boxes on user forms

Pauljj

Well-known Member
Joined
Mar 28, 2004
Messages
2,047
I have 8 list boxes on a user form but I want to make sure that the user always selects an option in each list box, my efforts have so far failed

I initially tried by checking the list box had been selected by using...

Code:
If LB6.Value = "" Then

That didn't work, as when I stepped through the code it suggested that LB6 was 'Null'

So I changed the code to

Code:
If LB6.Value = Null Then

That then suggested LB6 was ""

So I changed it again to say

Code:
If LB6.Value = Null or LB6.Value = "" Then

But it doesn't even acknowledge it now and skips past it

Any ideas please ?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try testing if the ListIndex property is greater than -1 (minus one)...
Code:
If LB6.ListIndex > -1 Then
  MsgBox "Something is selected"
End If
 
Upvote 0
Hi,
try this

Code:
Function IsSelected(ByVal Form As Object) As Boolean
	Dim i As Integer
	For i = 1 To 8
		With Form.Controls("LB" & i)
			IsSelected = CBool(.ListIndex <> -1)
			If Not IsSelected Then
				MsgBox "Please Make A Selection From ListBox " & .Name, 16, "Selection Required"
				.SetFocus
				Exit Function
			End If
		End With
	Next
End Function

call from your code

Code:
If Not IsSelected(Me) Then Exit Sub

Solution assumes that your Listboxes are named LB1, LB2 etc etc.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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