|
|||||||||||||||||||||
|
#1
|
|||
|
|||
|
"Select All" code in a ListBox
Hi,
I'm trying to get a code for a CheckBox which if selected will select all the values in a multi select ListBox. Code:
Private Sub CheckBox1_Click() If CheckBox1 = True Then For r = 1 To ListBox1.ListCount ListBox1.???= True Next End If End Sub Thanks, Eduard |
|
#2
|
||||
|
||||
|
Good morning Eduard
Quote:
Code:
Private Sub CheckBox1_Click() If CheckBox1 = True Then For r = 1 To ListBox1.ListCount ListBox1.Selected(r)= True Next End If End Sub DominicB
__________________
Download Ultimate Add-In v1.52 from www.dom-and-lis.co.uk 90+ Utilities, 200+ Sub utilities last updated 25th April 2008 Free!! Newest Tool : Worksheet Navigator (See all your worksheet tabs at once!!) Last edited by dominicb; 09-08-2008 at 09:26 AM. |
|
#3
|
|||
|
|||
|
Thanks Dominic. I was missing this Selected(r) code.
On the same subject: how can I turn the CheckBox value to False if at least 1 value in ListBox is False? Thanks, Eduard |
|
#4
|
||||
|
||||
|
Hi Eduard
Quote:
Code:
If CheckBox1 = True Then For r = 1 To ListBox1.ListCount If ListBox1.Selected(r)= False Then CheckBox1.Value=False Next ![]() HTH DominicB
__________________
Download Ultimate Add-In v1.52 from www.dom-and-lis.co.uk 90+ Utilities, 200+ Sub utilities last updated 25th April 2008 Free!! Newest Tool : Worksheet Navigator (See all your worksheet tabs at once!!) |
|
#5
|
||||
|
||||
|
You can always exit out of the loop on first occurance of an unselected item
Code:
If CheckBox1 = True Then
For r = 0 To ListBox1.ListCount-1
If ListBox1.Selected(r) = False Then
CheckBox1.Value = False
Exit For
End If
Next
End If
|
|
#6
|
|||
|
|||
|
Thanks for the thoughts guys. Unfortunately neither of the version is working. In Andy's case I can't change the value of the CheckBox to True as long as there is at least on False value in the ListBox
|
|
#7
|
||||
|
||||
|
This works for me. Userform with listbox and checkbox
Code:
Private m_blnSelectingAll As Boolean
Private Sub CheckBox1_Click()
Dim lngIndex As Long
m_blnSelectingAll = True
If CheckBox1.Value Then
For lngIndex = 0 To ListBox1.ListCount - 1
ListBox1.Selected(lngIndex) = True
Next
End If
m_blnSelectingAll = False
End Sub
Private Sub ListBox1_Change()
Dim lngIndex As Long
If CheckBox1.Value Then
If Not m_blnSelectingAll Then
For lngIndex = 0 To ListBox1.ListCount - 1
If Not ListBox1.Selected(lngIndex) Then
CheckBox1.Value = False
Exit For
End If
Next
End If
End If
End Sub
Private Sub UserForm_Initialize()
ListBox1.List = Array(1, 2, 3, 4, 5, 6, 7)
ListBox1.MultiSelect = fmMultiSelectMulti
End Sub
|
![]() |
| Bookmarks |
New topics in Excel Programming
|
|
|
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Copying from one workbook and pasting into another additon to code | snordr17 | Excel Programming | 1 | 06-30-2008 06:39 PM |
| MouseWheel on Listbox Crashes my Excel 2007 | ShredDude | Excel 2007 Help | 3 | 05-09-2008 02:19 AM |
| Running code while displaying a UserForm with vbModeless | PilgrimTim | Excel Programming | 7 | 08-06-2007 09:08 AM |
| Excel closes when using VBA to write VBA code | btoback | Excel Programming | 1 | 06-04-2007 04:42 PM |
| Problems understanding automated emailing code. | DDONNI | Excel Programming | 15 | 01-19-2007 12:26 PM |