Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Looping Listbox in Userform

I am using the following code which activates when the userform is changed.

Private Sub ListBox2_Change()

Dim NumberOfCategories As Integer
Dim PickCount As Integer

NumberOfCategories = Sheets("Parameters").Range("C6") - 1

'Determine the number of items selected
PickCount = 0
For x = 1 To NumberOfCategories
With ListBox2
If .Selected(x) Then
PickCount = PickCount + 1
End If
End With
Next x

'If "ALL" selected turn everything else off ("ALL" is the first line)
If ListBox2.Selected(0) And PickCount 0 Then
With ListBox2
For x = 1 To NumberOfCategories
.Selected(x) = 0
Next x
End With
End If

I have 2 problems. The code the count the number of items selected fails at
x=2. More importantly the "ALL" code causes the code to loop continuously.

Is there a way to set the change event to false at the bottom of the code to
prevent continuous looping? What other option do I have? Thank you in
advance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Looping Listbox in Userform

This will do your count of whats in the listbox
Sub listbox()
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
MsgBox i
End If
Next i
End Sub

"ZipCurs" wrote:

I am using the following code which activates when the userform is changed.

Private Sub ListBox2_Change()

Dim NumberOfCategories As Integer
Dim PickCount As Integer

NumberOfCategories = Sheets("Parameters").Range("C6") - 1

'Determine the number of items selected
PickCount = 0
For x = 1 To NumberOfCategories
With ListBox2
If .Selected(x) Then
PickCount = PickCount + 1
End If
End With
Next x

'If "ALL" selected turn everything else off ("ALL" is the first line)
If ListBox2.Selected(0) And PickCount 0 Then
With ListBox2
For x = 1 To NumberOfCategories
.Selected(x) = 0
Next x
End With
End If

I have 2 problems. The code the count the number of items selected fails at
x=2. More importantly the "ALL" code causes the code to loop continuously.

Is there a way to set the change event to false at the bottom of the code to
prevent continuous looping? What other option do I have? Thank you in
advance.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Looping Listbox in Userform

Mike,

Thanks for your response. While this is not exactly what I am after, it is
a step in the right direction. I forgot to mention that this is a multipick
listbox, so I really need to know how many items have been selected. With
your input, I have figured that one out.

The real issue is getting the Event_Change feature to work only when manual
changes are made rather than automated ones. It seems like you should be
able to prevent this.

Again, any help would be appreciated.

"Mike" wrote:

This will do your count of whats in the listbox
Sub listbox()
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
MsgBox i
End If
Next i
End Sub

"ZipCurs" wrote:

I am using the following code which activates when the userform is changed.

Private Sub ListBox2_Change()

Dim NumberOfCategories As Integer
Dim PickCount As Integer

NumberOfCategories = Sheets("Parameters").Range("C6") - 1

'Determine the number of items selected
PickCount = 0
For x = 1 To NumberOfCategories
With ListBox2
If .Selected(x) Then
PickCount = PickCount + 1
End If
End With
Next x

'If "ALL" selected turn everything else off ("ALL" is the first line)
If ListBox2.Selected(0) And PickCount 0 Then
With ListBox2
For x = 1 To NumberOfCategories
.Selected(x) = 0
Next x
End With
End If

I have 2 problems. The code the count the number of items selected fails at
x=2. More importantly the "ALL" code causes the code to loop continuously.

Is there a way to set the change event to false at the bottom of the code to
prevent continuous looping? What other option do I have? Thank you in
advance.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping Listbox in Userform

Does this code do what you want?

' These two lines go in the (General)(Declarations) section
Dim PickCount As Long
Dim Processing As Boolean

Private Sub ListBox2_Change()
Dim X As Long
If Processing Then Exit Sub
With ListBox2
PickCount = 0
Processing = True
If .ListIndex 0 Then .Selected(0) = False
If .Selected(0) Then
For X = 1 To .ListCount - 1
.Selected(X) = False
Next
PickCount = -1
Else
For X = 1 To .ListCount - 1
If .Selected(X) Then PickCount = PickCount + 1
Next
End If
Processing = False
End With
Label1.Caption = PickCount
End Sub

Rick



"ZipCurs" wrote in message
...
Mike,

Thanks for your response. While this is not exactly what I am after, it
is
a step in the right direction. I forgot to mention that this is a
multipick
listbox, so I really need to know how many items have been selected. With
your input, I have figured that one out.

The real issue is getting the Event_Change feature to work only when
manual
changes are made rather than automated ones. It seems like you should be
able to prevent this.

Again, any help would be appreciated.

"Mike" wrote:

This will do your count of whats in the listbox
Sub listbox()
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
MsgBox i
End If
Next i
End Sub

"ZipCurs" wrote:

I am using the following code which activates when the userform is
changed.

Private Sub ListBox2_Change()

Dim NumberOfCategories As Integer
Dim PickCount As Integer

NumberOfCategories = Sheets("Parameters").Range("C6") - 1

'Determine the number of items selected
PickCount = 0
For x = 1 To NumberOfCategories
With ListBox2
If .Selected(x) Then
PickCount = PickCount + 1
End If
End With
Next x

'If "ALL" selected turn everything else off ("ALL" is the first line)
If ListBox2.Selected(0) And PickCount 0 Then
With ListBox2
For x = 1 To NumberOfCategories
.Selected(x) = 0
Next x
End With
End If

I have 2 problems. The code the count the number of items selected
fails at
x=2. More importantly the "ALL" code causes the code to loop
continuously.

Is there a way to set the change event to false at the bottom of the
code to
prevent continuous looping? What other option do I have? Thank you in
advance.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Looping Listbox in Userform

I forgot to mention... put a Label control on your form... the code updates
this Label (named Label1) with the value of PickCount (that is, it displays
the number of selected items). I wasn't sure what you wanted PickCount to be
when ALL was selected, so I simply set it to -1 so that it could easily be
tested for.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
Does this code do what you want?

' These two lines go in the (General)(Declarations) section
Dim PickCount As Long
Dim Processing As Boolean

Private Sub ListBox2_Change()
Dim X As Long
If Processing Then Exit Sub
With ListBox2
PickCount = 0
Processing = True
If .ListIndex 0 Then .Selected(0) = False
If .Selected(0) Then
For X = 1 To .ListCount - 1
.Selected(X) = False
Next
PickCount = -1
Else
For X = 1 To .ListCount - 1
If .Selected(X) Then PickCount = PickCount + 1
Next
End If
Processing = False
End With
Label1.Caption = PickCount
End Sub

Rick



"ZipCurs" wrote in message
...
Mike,

Thanks for your response. While this is not exactly what I am after, it
is
a step in the right direction. I forgot to mention that this is a
multipick
listbox, so I really need to know how many items have been selected.
With
your input, I have figured that one out.

The real issue is getting the Event_Change feature to work only when
manual
changes are made rather than automated ones. It seems like you should be
able to prevent this.

Again, any help would be appreciated.

"Mike" wrote:

This will do your count of whats in the listbox
Sub listbox()
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
MsgBox i
End If
Next i
End Sub

"ZipCurs" wrote:

I am using the following code which activates when the userform is
changed.

Private Sub ListBox2_Change()

Dim NumberOfCategories As Integer
Dim PickCount As Integer

NumberOfCategories = Sheets("Parameters").Range("C6") - 1

'Determine the number of items selected
PickCount = 0
For x = 1 To NumberOfCategories
With ListBox2
If .Selected(x) Then
PickCount = PickCount + 1
End If
End With
Next x

'If "ALL" selected turn everything else off ("ALL" is the first line)
If ListBox2.Selected(0) And PickCount 0 Then
With ListBox2
For x = 1 To NumberOfCategories
.Selected(x) = 0
Next x
End With
End If

I have 2 problems. The code the count the number of items selected
fails at
x=2. More importantly the "ALL" code causes the code to loop
continuously.

Is there a way to set the change event to false at the bottom of the
code to
prevent continuous looping? What other option do I have? Thank you
in
advance.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default Looping Listbox in Userform

Hello Rick,

Thanks for your detailed response. Unfortunately, I have been unable to
test this in detail due to other distractions and the fact that I implemented
a kludge that basically gets what I need done.

The question I should have asked from the beginning is how to limit the
number of items selected on a multiple select listbox. Once four has been
reached, I want to disable add, while still allowing items to be removed.

Thanks

"Rick Rothstein (MVP - VB)" wrote:

I forgot to mention... put a Label control on your form... the code updates
this Label (named Label1) with the value of PickCount (that is, it displays
the number of selected items). I wasn't sure what you wanted PickCount to be
when ALL was selected, so I simply set it to -1 so that it could easily be
tested for.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
Does this code do what you want?

' These two lines go in the (General)(Declarations) section
Dim PickCount As Long
Dim Processing As Boolean

Private Sub ListBox2_Change()
Dim X As Long
If Processing Then Exit Sub
With ListBox2
PickCount = 0
Processing = True
If .ListIndex 0 Then .Selected(0) = False
If .Selected(0) Then
For X = 1 To .ListCount - 1
.Selected(X) = False
Next
PickCount = -1
Else
For X = 1 To .ListCount - 1
If .Selected(X) Then PickCount = PickCount + 1
Next
End If
Processing = False
End With
Label1.Caption = PickCount
End Sub

Rick



"ZipCurs" wrote in message
...
Mike,

Thanks for your response. While this is not exactly what I am after, it
is
a step in the right direction. I forgot to mention that this is a
multipick
listbox, so I really need to know how many items have been selected.
With
your input, I have figured that one out.

The real issue is getting the Event_Change feature to work only when
manual
changes are made rather than automated ones. It seems like you should be
able to prevent this.

Again, any help would be appreciated.

"Mike" wrote:

This will do your count of whats in the listbox
Sub listbox()
For i = 0 To ListBox2.ListCount - 1
If ListBox2.Selected(i) Then
MsgBox i
End If
Next i
End Sub

"ZipCurs" wrote:

I am using the following code which activates when the userform is
changed.

Private Sub ListBox2_Change()

Dim NumberOfCategories As Integer
Dim PickCount As Integer

NumberOfCategories = Sheets("Parameters").Range("C6") - 1

'Determine the number of items selected
PickCount = 0
For x = 1 To NumberOfCategories
With ListBox2
If .Selected(x) Then
PickCount = PickCount + 1
End If
End With
Next x

'If "ALL" selected turn everything else off ("ALL" is the first line)
If ListBox2.Selected(0) And PickCount 0 Then
With ListBox2
For x = 1 To NumberOfCategories
.Selected(x) = 0
Next x
End With
End If

I have 2 problems. The code the count the number of items selected
fails at
x=2. More importantly the "ALL" code causes the code to loop
continuously.

Is there a way to set the change event to false at the bottom of the
code to
prevent continuous looping? What other option do I have? Thank you
in
advance.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Listbox Looping Bret Excel Programming 1 October 24th 06 02:38 AM
userform listbox cannot get listbox.value to transfer back to main sub [email protected] Excel Programming 1 May 17th 06 09:44 PM
Looping to fill ListBox davidm Excel Programming 2 January 5th 06 08:39 AM
Looping procedure calls userform; how to exit loop (via userform button)? KR Excel Programming 6 July 27th 05 12:57 PM
Looping through listbox controls Vince Excel Programming 11 April 12th 05 04:40 PM


All times are GMT +1. The time now is 05:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"