ExcelTip.com
Account Icon Account Icon Account Icon
ExcelTip.com

Go Back   Excel Help Forum > Usenet Groups > Excel Programming

Notices

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 09-08-2008, 07:33 AM
Eduard Eduard is offline
Registered User
 
Join Date: 30 Aug 2005
Posts: 26
Eduard is on a distinguished road
"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
Can someone help me to get the correct code to turn the values in the ListBox to True?

Thanks,
Eduard
Reply With Quote
  #2  
Old 09-08-2008, 08:05 AM
dominicb's Avatar
dominicb dominicb is offline
Forum Moderator
 
Join Date: 25 Jan 2005
Location: Lancashire, England
Posts: 2,682
dominicb will become famous soon enough dominicb will become famous soon enough
Smile

Good morning Eduard
Quote:
Originally Posted by Eduard View Post
Can someone help me to get the correct code to turn the values in the ListBox to True?
Something like this should work OK :
Code:
Private Sub CheckBox1_Click()

If CheckBox1 = True Then
For r = 1 To ListBox1.ListCount
ListBox1.Selected(r)= True
Next

End If

End Sub
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!!)

Last edited by dominicb; 09-08-2008 at 09:26 AM.
Reply With Quote
  #3  
Old 09-08-2008, 08:57 AM
Eduard Eduard is offline
Registered User
 
Join Date: 30 Aug 2005
Posts: 26
Eduard is on a distinguished road
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
Reply With Quote
  #4  
Old 09-08-2008, 09:25 AM
dominicb's Avatar
dominicb dominicb is offline
Forum Moderator
 
Join Date: 25 Jan 2005
Location: Lancashire, England
Posts: 2,682
dominicb will become famous soon enough dominicb will become famous soon enough
Smile

Hi Eduard
Quote:
Originally Posted by Eduard View Post
On the same subject: how can I turn the CheckBox value to False if at least 1 value in ListBox is False?
I suppose you could simply loop through the listbox again, and every time an instance of False is found, set CheckBox1 to false. This might be considered inefficient in that it is needlessly accessing the checkbox every time a false is found - rather than just if at least one is false, but for the number of items in the listbox, it saves introducing an extra variable.

Code:
If CheckBox1 = True Then
For r = 1 To ListBox1.ListCount
If ListBox1.Selected(r)= False Then CheckBox1.Value=False
Next
BTW, just spotted my little typo below (now corrected) - well deciphered

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!!)
Reply With Quote
  #5  
Old 09-08-2008, 09:41 AM
Andy Pope's Avatar
Andy Pope Andy Pope is online now
Forum Moderator
 
Join Date: 10 May 2004
Location: Essex, UK
Posts: 2,303
Andy Pope will become famous soon enough Andy Pope will become famous soon enough
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
__________________
Cheers
Andy
Reply With Quote
  #6  
Old 09-08-2008, 10:21 AM
Eduard Eduard is offline
Registered User
 
Join Date: 30 Aug 2005
Posts: 26
Eduard is on a distinguished road
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
Reply With Quote
  #7  
Old 09-08-2008, 10:30 AM
Andy Pope's Avatar
Andy Pope Andy Pope is online now
Forum Moderator
 
Join Date: 10 May 2004
Location: Essex, UK
Posts: 2,303
Andy Pope will become famous soon enough Andy Pope will become famous soon enough
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
__________________
Cheers
Andy
Reply With Quote
Reply

Bookmarks

New topics in Excel Programming


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

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


All times are GMT -4. The time now is 04:48 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0