R
rob nobel
I have the code (set out below the question), that fills a ListBox to enter
the item to the active cell.
The line "Set AllCells = Sheet11.Range("B5:B54")" refers to the items but I
would like this procedure to pull the items from 8 different ranges
depending on what the value is in column F (on the same row where the cell
is active that prompts this procedure).
In other words, if cell I38 is activated, this procedure should run
automatically and determine what the value is in F38, to in turn determine
which range to select to fill the ListBox.
So, if F38 = "Gen", the range is Sheet11.Range("B5:B54").
If F38 = "Min", the range is Sheet11.Range("G5:G54").
If F38 = "Bld", the range is Sheet11.Range("L5:L54"),
and so on for the other 5 options.
Can someone please help me with this? (Also, how to activate the procedure
when any of the cells I26 to I1525 is activated.)
Rob
Sub mSelectCode()
Dim AllCells As Range, Cell As Range
Dim NoDupes As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item
' Where the items are.
Set AllCells = Sheet11.Range("B5:B54")
Sub mFillCodeList()
On Error Resume Next
For Each Cell In AllCells
NoDupes.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
' Update the labels on UserForm1
With ufSelectCode
.Label1.Caption = "Total Items: " & AllCells.Count
.Label2.Caption = "Unique Items: " & NoDupes.Count
End With
' Sort the collection (optional)
For i = 1 To NoDupes.Count - 1
For j = i + 1 To NoDupes.Count
If NoDupes(i) > NoDupes(j) Then
Swap1 = NoDupes(i)
Swap2 = NoDupes(j)
NoDupes.Add Swap1, before:=j
NoDupes.Add Swap2, before:=i
NoDupes.Remove i + 1
NoDupes.Remove j + 1
End If
Next j
Next i
' Add the sorted, non-duplicated items to a ListBox
For Each Item In NoDupes
ufSelectCode.ListBox1.AddItem Item
Next Item
' Show the ufSelectCode
ufSelectCode.Show
End Sub
the item to the active cell.
The line "Set AllCells = Sheet11.Range("B5:B54")" refers to the items but I
would like this procedure to pull the items from 8 different ranges
depending on what the value is in column F (on the same row where the cell
is active that prompts this procedure).
In other words, if cell I38 is activated, this procedure should run
automatically and determine what the value is in F38, to in turn determine
which range to select to fill the ListBox.
So, if F38 = "Gen", the range is Sheet11.Range("B5:B54").
If F38 = "Min", the range is Sheet11.Range("G5:G54").
If F38 = "Bld", the range is Sheet11.Range("L5:L54"),
and so on for the other 5 options.
Can someone please help me with this? (Also, how to activate the procedure
when any of the cells I26 to I1525 is activated.)
Rob
Sub mSelectCode()
Dim AllCells As Range, Cell As Range
Dim NoDupes As New Collection
Dim i As Integer, j As Integer
Dim Swap1, Swap2, Item
' Where the items are.
Set AllCells = Sheet11.Range("B5:B54")
Sub mFillCodeList()
On Error Resume Next
For Each Cell In AllCells
NoDupes.Add Cell.Value, CStr(Cell.Value)
Next Cell
On Error GoTo 0
' Update the labels on UserForm1
With ufSelectCode
.Label1.Caption = "Total Items: " & AllCells.Count
.Label2.Caption = "Unique Items: " & NoDupes.Count
End With
' Sort the collection (optional)
For i = 1 To NoDupes.Count - 1
For j = i + 1 To NoDupes.Count
If NoDupes(i) > NoDupes(j) Then
Swap1 = NoDupes(i)
Swap2 = NoDupes(j)
NoDupes.Add Swap1, before:=j
NoDupes.Add Swap2, before:=i
NoDupes.Remove i + 1
NoDupes.Remove j + 1
End If
Next j
Next i
' Add the sorted, non-duplicated items to a ListBox
For Each Item In NoDupes
ufSelectCode.ListBox1.AddItem Item
Next Item
' Show the ufSelectCode
ufSelectCode.Show
End Sub