Search textbox to listbox multi columns - excel

I have a code work perfectly, that search the items in worksheets then the userform textbox display in the listbox. but somehow it show only in one columns in listbox.
I want to show 4 columns in lisbox.
Code:
Private Sub TextBox1_Change()
Dim search As Variant
Dim textbox As Variant
Dim index As Variant
Dim item As Variant
Dim result As Variant
Dim match As Integer
With Me.ListBox1
.RowSource = ""
End With
On Error GoTo skip match = 0 ListBox1.Clear
With Range("Forcast")
Set textbox = .Find(TextBox1, LookIn:=xlValues, lookat:=xlPart)
If Not textbox Is Nothing Then
index = textbox.Address
Do
result = Sheets("Report").Cells(Range(textbox.Address).Row, 1).Value
For Each item In ListBox1.List
If item = result Then match = 1
Next item
If match = 0 Then ListBox1.AddItem result
listbox1.ColumnCount = 4
Set textbox = .FindNext(textbox)
match = 0
Loop While Not textbox Is Nothing And textbox.Address <> index
End If End With
End Sub

2 ways to add items to a ListBox:
Option Explicit
Private Sub UserForm_Initialize()
With ListBox1
.ColumnCount = 4 '---------------------------------------------
'add a 2 dimensional array
.List = Worksheets(1).Range("A1:D3").Value2 '3 rows, 4 columns
'add a 2 dimensional array
.List = Worksheets(1).Range("A1:D1").Value2 '1 rows, 4 columns
.ColumnCount = 1 '---------------------------------------------
'add a 1 dimensional array
.List = Array(1, 2, 3, 4) '4 rows, 1 column
'same as above: '4 rows, 1 column
.AddItem "1"
.AddItem "2"
.AddItem "3"
.AddItem "4"
End With
End Sub

Related

Can I change in Excel VBA combobox.list range?

Is there any option in Excel VBA which allows me to change range of combobox?
See example
I would like to change number 5 in Variant/Variant(0 to 3, 0 to 5).
Combobox is filled with this code:
Private Sub UserForm_Activate()
Dim x As Range
With Worksheets("Distributori")
Set x = .Range("A2", .Range("F1000").End(xlUp))
End With
ComboBox1.RowSource = "Distributori!" & x.Address
ComboBox1.ListIndex = 0
Me.ComboBox1.TextColumn = 2
End Sub
Try the next (adapted) code, please:
Dim x As Range, arrList As Variant
With Worksheets("Distributori")
Set x = .Range("A2", .Range("F1000").End(xlUp))
End With
arrList = x.Value
With ComboBox1
.ColumnCount = 6
.list = arrList
.ListIndex = 0
.TextColumn = 2
End With

Filter a Listbox based on 2 Combobox(es)

So i'm very new to coding and want to learn how can i filter a listbox based on selection on 2 comboboxes. So what i want to do is when VBA initializes the entire range displays on the listbox and when 1 combobox is selected it filters down the range and when the second combobox is selected it filters down even more and both comboboxes can be used individually. but i couldnt find anything online either for VBA excel or something similar.
Private Sub ComboBox1_Change()
Dim Database(1 To 100, 1 To 4)
Dim my_range As Integer
Dim colum As Byte
On Error Resume Next
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("MASTER")
sh.Range("G5").AutoFilter Field:=7, Criteria1:=Me.ComboBox1.Value
For i = 5 To sh.Range("G100000").End(xlUp).Row
If sh.Cells(i, 1) = Me.ComboBox1 Then
my_range = my_range + 1
For colum = 1 To 5
Database(my_range, colum) = sh.Cells(i, colum)
Next colum
End If
Next i
Me.ListBox1.List = Database
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
.Clear
.AddItem ""
.AddItem "L461"
.AddItem "L462"
.AddItem "L463"
.AddItem "L464"
.AddItem "L465"
End With
End Sub

Listbox not showing the values that were populated in it using Listbox.List method

After running the Userform_Initialize() event, there would be nothing populated in the listbox as shown below:
There should be 11 columns populating the listbox based on the excel table below:
The code ran:
Private Sub UserForm_Initialize()
Dim Total_rows_FoilProfile As Long
Dim row As Range, i As Long
Total_rows_FoilProfile = TotalRowsCount(ThisWorkbook.Name, "Foil Profile", "tblFoilProfile")
ReDim MyArr(0 To Total_rows_FoilProfile - 1)
For Each row In ThisWorkbook.Worksheets("Foil Profile").ListObjects("tblFoilProfile").Range.SpecialCells(xlCellTypeVisible).Rows
MyArr(i) = row.Value
i = i + 1
Next row
lbxFoilInfoDisplay.List = MyArr
frmFoilPanel.Show
The properties of the listbox:
You can populate each list row and then add the columns to it:
Option Explicit
Private Sub UserForm_Initialize()
Dim tblFoilProfile As ListObject
Set tblFoilProfile = ThisWorkbook.Worksheets("Foil Profile").ListObjects("tblFoilProfile")
Dim i As Long
lbxFoilInfoDisplay.Clear
Dim iListRow As Range
For Each iListRow In tblFoilProfile.DataBodyRange.SpecialCells(xlCellTypeVisible).Rows
With Me.lbxFoilInfoDisplay
.AddItem iListRow.Cells(1, 1).Value 'add first value (column 1)
Dim iCol As Long
For iCol = 2 To iListRow.Columns.Count 'add all other columns to that row
.list(i, iCol) = iListRow.Cells(1, iCol).Value '.Value for unformatted value or .Text to show it in the same format as in the cell
Next iCol
i = i + 1
End With
Next iListRow
End Sub
Note here is a nice guide how to work with list objects.

Excel VBA loop through combobox with select case statements

I have a worksheet with 6 activex comboboxes.
Combobox1 has 21 choiches.
Combobox2 is dependent on Combobox1, number of choices vary.
Combobox3 has 2 choices.
Combbox4 is dependent on Combobox3 and has 21 choices.
Combobox5 has 21 choiches.
Combobox6 is dependent on Combobox5, number of choices vary.
I would like to loop through combobox1 - value1 and combobox2 - value1.
Then I would like to loop through combobox3 - value 1 and combobox4 - value1.
I would like to loop through combobox5 - value1 and combobox6 - value1.
I am using vlookup to based on the linked cells of the different comboboxes. The code I have at the moment only loops through the cell values of combobox1 and combobox2. I would like to physically change the value in the combobox, from value1 to lastvalue.
Which would be Combobox1 - value1, combobox2 value1 to last value, combobox3 first value, combobox4 - value1, combobox 5, value1 and finally combobox6 - value1 to last value.
Sub Demo()
Dim Ws As Worksheet
Dim shp As Shape
Dim cb As ComboBox
Set Ws = ActiveSheet
For Each shp In Ws.Shapes
With shp
Select Case .Type
Case msoFormControl
If .FormControlType = xlDropDown Then
If .ControlFormat.Value = 0 Then
MsgBox .Name & " = "
Else
MsgBox .Name & " = " & .ControlFormat.List(.ControlFormat.Value)
End If
End If
Case msoOLEControlObject
If .OLEFormat.progID = "Forms.ComboBox.1" Then
Set cb = .OLEFormat.Object.Object
MsgBox cb.Name & " = " & cb.Value
End If
End Select
End With
Next
End Sub
The code above gives me the values of my 6 activex comboboxes.
Sub try()
Dim Ws As Worksheet
Set Ws = Worksheets("Sheet1")
Count = 0
For Each OleObj In Ws.OLEObjects
If OleObj.OLEType = xlOLEControl Then
If TypeName(OleObj.Object) = "ComboBox" Then
Count = Count + 1
End If
End If
Next OleObj
MsgBox "Number of ComboBoxes :" & Count
End Sub
This code counts the amount of comboboxes in a sheet! Maybe it can be adapted to increment each combobox?
I am thinking about something like this:
Sub Test()
Select Case Me.Form
Case "Stockholms län"
Me.Kommun1.RowSource = "Stockholms_län"
' Code for each loop where combobox1 is "Stockholm län" and Combobox2
' is a named range.
Case "Skåne län"
Me.Kommun1.RowSource = "Skåne_län"
' Code for each loop where combobox1 is "Skåne län" and Combobox2 is
' a named range.
End Select
End sub
I can manually set the values of combobox1 and combobox2.
Sub WantToLoop()
Dim län1 As String
Dim kommun1 As String
Dim län2 As String
Dim kommun2 As String
ThisWorkbook.Sheets("Test").län1 = "Skåne län"
ThisWorkbook.Sheets("Test").kommun1 = "Bjuv"
End Sub
The code above kind of works but I can't do select cases for several hundred of choices. How can I loop this?
I am getting closer but now I am setting the value of the combobox. I want to access the value of the combobox.
Sub try()
Dim i As Integer
Dim j As Integer
For i = 1 To 2
Sheets("Test").Shapes("Län" & i).OLEFormat.Object.Object = "item" & i
Sheets("Test").Shapes("Kommun" & i).OLEFormat.Object.Object = "item" & i
Next
End sub
Sub IterateComboBox()
Dim i As Long
With Sheets("Jämföra").Län1
For i = 0 To .ListCount - 1
'Debug.Print .List(i)
.Value = .List(i)
Next
End With
End Sub
This code does what I want. How can I turn this into a select case?
Sub Try2()
Dim k As Integer
Dim l As Integer
Dim m As Integer
Dim n As Integer
Sheets("Test").Län1.ListIndex = 0
For l = 0 To 25
Sheets("Test").Kommun1.ListIndex = l
Sheets("Test").Län2.ListIndex = 0
For n = 0 To 25
Sheets("Test").Kommun2.ListIndex = n
Application.ScreenUpdating = True
Sheets("Score").Select
Dim LR As Long
LR = Cells(Rows.Count, 1).End(xlUp).Row + 1
'Län1, Kommun1 OK
Cells(LR, "A").Value = Sheets("Test").Range("G5").Value
Cells(LR, "B").Value = Sheets("Test").Range("G6").Value
Next
Next
End Sub
This code loops through combobox1 value1, combobox2 all values, combobox3 value1 and combobox4 all values. How can I turn this into a case?
Or how do I turn this into a function where I can pass the values of Län1, Kommun1, Län2 and Kommun2 into the function??

What format must cells have for datarange for combobox in VBA?

When I select a value in ComboBox1, ComboBox2 is automatically filled with data from a specified range. On selecting a value in ComboBox2 a corresponding value will be placed in a TextBox.
It appears that when I have only numbers in the ComboBox value the code will not function.
If the value consists of letters or numbers AND letters everything works just fine.
I tried different formatting of the cells in the range, but no success. Finding an answer on internet is problematic as I don't know the keywords for searching.
Here's my code so far:
Private Sub ComboBox1_Change()
Dim index1 As Integer
Dim cLoc1 As Range
Dim index2 As Integer
Dim cLoc2 As Range
Dim ws As Worksheet
Set ws = Worksheets("Formlists")
index1 = ComboBox1.ListIndex
'index2 = ComboBox2.ListIndex
ComboBox2.Clear
ComboBox3.Clear
Select Case index1
Case Is = 0
With ComboBox2
For Each cLoc1 In ws.Range("Codelist1")
With Me.ComboBox2
.AddItem cLoc1.Value
End With
Next cLoc1
End With
Case Is = 1
With ComboBox2
For Each cLoc1 In ws.Range("Codelist2")
With Me.ComboBox2
.AddItem cLoc1.Value
End With
Next cLoc1
End With
End Select
End Sub 
Private Sub combobox2_change()
Dim index2 As Integer
Dim ws As Worksheet
Set ws = Worksheets("Formlists")
index2 = ComboBox1.ListIndex
Select Case index2
Case Is = 0
With TextBox4
For i = 1 To 10
If ws.Cells(i + 1, Columns(90).Column).Value = ComboBox2.Value Then
TextBox4.Value = ws.Cells(i + 1, Columns(91).Column).Value
Else
'do nothing
End If
Next i
End With
Case Is = 1
With TextBox4
For i = 1 To 10
If ws.Cells(i + 1, Columns(92).Column).Value = ComboBox2.Value Then
TextBox4.Value = ws.Cells(i + 1, Columns(93).Column).Value
Else
'do nothing
End If
Next i
End With
End Select
End Sub
Is there a way to let the code/ComboBox accept any inputformat?
Thanks in advance
Rob
It appears you just need to coerce the values to strings in the code:
If CStr(ws.Cells(i + 1, 90).Value) = ComboBox2.Value Then
and similarly with the other test. Note there isn't any point in using Columns(90).Column rather than just 90. ;)

Resources