Tmarques
New Member
- Joined
- Mar 29, 2017
- Messages
- 1
Hi all
Wondering if someone could help with the following issue.
I have a userform that is used to populate values to a sheet.
This uses a preliminary selection from a ComboBox value and its supposed to show on a ListBox the values categorized with the text coming from the ComboBox.
The values are available in a regular excel sheet:
<tbody>
</tbody>
Hence, whenever in the Combobox I select a column A value the Listbox will show the values from columns B and C.
I've managed to get the ListBox to show only values from the column A selection, albeit for multiple instances I only have the first instance found showing.
My idea would be to make a loop that would run for each single instance where the values in column A are found and add the values from columns B to C to the listbox
Below follows the code so far:
Thanks in advance for the support.
Wondering if someone could help with the following issue.
I have a userform that is used to populate values to a sheet.
This uses a preliminary selection from a ComboBox value and its supposed to show on a ListBox the values categorized with the text coming from the ComboBox.
The values are available in a regular excel sheet:
jupe | jp001 | 18.- | |
jupe | jp002 | 26.- | |
Pantalon | pt001 | 16.- | |
Manteaux | mt001 | 20.- | |
Manteaux | mt002 | 31.- |
<tbody>
</tbody>
Hence, whenever in the Combobox I select a column A value the Listbox will show the values from columns B and C.
I've managed to get the ListBox to show only values from the column A selection, albeit for multiple instances I only have the first instance found showing.
My idea would be to make a loop that would run for each single instance where the values in column A are found and add the values from columns B to C to the listbox
Below follows the code so far:
Code:
Private Sub UserForm_Initialize()
Facture.Activate
TextBox1 = "1"
Dim Prix As Worksheet
Set ws = ThisWorkbook.Sheets("Prix")
'creation of an array to populate the combobox with single values by removing duplicates
wsLR = ws.Cells(Rows.Count, 1).End(xlUp).Row
firstTime = True
For x = 2 To wsLR
If ws.Cells(x, 1) <> "" And (InStr(unik, "|" & ws.Cells(x, 1) & "|") = 0) Then
If firstime = True Then
unik = "|" & unik & ws.Cells(x, 1) & "|"
firstTime = False
Else
unik = unik & "|" & ws.Cells(x, 1) & "|"
End If
End If
Next x
'add values to combobox
myArray = Split(unik, "|")
ComboBox1.Clear
For Each Cell In myArray
If Cell <> "" Then
ComboBox1.AddItem Cell
End If
Next Cell
End Sub
Private Sub ComboBox1_Change()
Dim searchResult As Range
Dim rngToFind As Range
Dim valToFind As Variant
Dim arrClearList()
'search for values based on the combobox selection
valToFind = ComboBox1.Value
With Worksheets("Prix")
Set searchResult = .Columns("A")
End With
Set rngToFind = searchResult.Find(What:=valToFind, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not rngToFind Is Nothing Then
ListBox1.Clear
' add values to list__ This is where I have issues since I'm unable to develop a loop which would run the below script for all instances where the value of the combobox is found in the Worksheet.
ListBox1.AddItem
With ListBox1
.List(.ListCount - 1, 0) = rngToFind.Offset(0, 1).Value
.List(.ListCount - 1, 1) = rngToFind.Offset(0, 2).Value
.List(.ListCount - 1, 2) = rngToFind.Offset(0, 3).Value
End With
End If
End Sub
Thanks in advance for the support.