Hi,
I'm attempting to prioritize what is selected first in List box for example; I have source that has 1,2,3,4 The user will select 2,1,and 3. When the output comes out into a cell, I want it to show 2,1,3 not 1,2,3. Here is the code that I have; however, I'm getting a subscript Out of Range on
Here is my Code at the top of the Userform:
Then I have a Change in as well:
I'm attempting to prioritize what is selected first in List box for example; I have source that has 1,2,3,4 The user will select 2,1,and 3. When the output comes out into a cell, I want it to show 2,1,3 not 1,2,3. Here is the code that I have; however, I'm getting a subscript Out of Range on
Code:
If listSelect(i) = totalSelect Then
Here is my Code at the top of the Userform:
Code:
Option Explicit
Dim listSelect() As Integer, totalSelect As Integer, eventsEnabler As Integer
Private Sub Userform_Intialize()
' Disable DEPListBox_Change() code
eventsEnabler = 1
With DEPListBox
' .AddItems here
End With
ReDim listSelect(0 To DEPListBox.ListCount - 1)
totalSelect = 0
' Enable _Change() code
eventsEnabler = eventsEnabler - 1
End Sub
Then I have a Change in as well:
Code:
Private Sub DEPListBox_Change()Dim i As Integer, j As Integer, indexStart As Integer, indexEnd As Integer, stepDir As Integer
' When setting up DEPListBox, set eventsEnabler to > 0 to prevent this code from executing!
If eventsEnabler = 0 Then
' Start by assuming we'll search for new selections top-to-bottom
indexStart = 0
indexEnd = DEPListBox.ListCount - 1
' This block determines direction of additional selections (up or down)
For i = 0 To DEPListBox.ListCount - 1
' If we've hit the previous last selection, we already know the answer (top-to-bottom)
If listSelect(i) = totalSelect Then Exit For
' If we hit a new selection before finding the previous last selection,
' then we'll change the search for new selections to be bottom-to-top
If listSelect(i) = 0 And DEPListBox.Selected(i) Then
indexStart = indexEnd
indexEnd = 0
Exit For
End If
Next i
stepDir = Sgn(indexEnd - indexStart)
If stepDir = 0 Then stepDir = 1
' Update selection list in listSelect()
For i = indexStart To indexEnd Step stepDir
If DEPListBox.Selected(i) = True Then
' Newly selected item: place selection number in listSelect() and update totalSelect
If listSelect(i) = 0 Then
listSelect(i) = totalSelect + 1
totalSelect = totalSelect + 1
End If
Else
' Deselected item: remove its selection number, and update all others to compensate.
If listSelect(i) > 0 Then
For j = 0 To DEPListBox.ListCount - 1
If listSelect(j) > listSelect(i) Then listSelect(j) = listSelect(j) - 1
Next j
listSelect(i) = 0
totalSelect = totalSelect - 1
End If
End If
Next i
' At this point, totalSelect and listSelect() are updated for you to act upon.
End If
End Sub