Ok guys, so I had a bit of trouble with my first post regarding this problem and I had received a ton of help. However we are still running into some trouble.
What I am trying to do: I have 4 listboxes on 4 different sheets. The list boxes are looking at a column that has a list of items. The Items where the listboxes are referencing have sub items. I want the list box (when clicking the item inside the listbox), to show the sub items in text boxes that I have on the sheet. I had some code that worked in the beginning, however, when I try to use it on multiple sheets it does not work.
The first block of code is what I was originally using, but I came on here and had some help that gave me the last 2 blocks of code; that should be working but are not.
Sub ListBox1_Change()
Dim idx As Long
If idx <> -1 Then
idx = Sheets("sheet1").Shapes("TextBox 1").OLEFormat.Object.ListIndex
Sheets("sheet1").Shapes("TextBox 1").OLEFormat.Object.Text = Sheets("sheet1").Range("Q" & idx + 1).Value
End If
End Sub
This block of code is specific to each sheet. There are about 30 total text boxes that I will be referencing on each sheet. So in actuality the below will have 30 items.
Sub ListBox1_Change()
Handle_Change Me, "ListBox1", "Texbox1(sub-item)", "W"
Handle_Change Me, "ListBox1", "Texbox2(sub-item)", "X"
Handle_Change Me, "ListBox1", "Texbox3(sub-item)", "Y"
Handle_Change Me, "ListBox1", "Texbox4(sub-item)", "Z"
Handle_Change Me, "ListBox1", "Texbox5(sub-item)", "AA"
Handle_Change Me, "ListBox1", "Texbox6(sub-item)", "AB"
End Sub
This is code that stay in the VBA Module
Sub Handle_Change(sht As Worksheet, lbName, tbName, colAddr)
Dim idx As Long, lb As msforms.ListBox
Set lb = sht.Shapes(lbName).OLEFormat.Object.Object
idx = lb.ListIndex
If idx <> -1 Then
sht.Shapes(tbName).OLEFormat.Object.Object.Text = _
sht.Range(colAddr & idx + 1).Value
End If
End Sub
When I try to run it with the new code (last 2 blocks above) I get an error that says: "Compile error: User-defined type not defined" and highlights the second line of code on the Handle_change ", lb As msforms.Listbox".
Thanks for your time,
Good day
OK - I think this is what you need.
This goes in the worksheet code module: you need on event handler per listbox.
Sub ListBox1_Change()
Handle_Change Me, "ListBox1", _
Array("Textbox1", "Textbox2", "Textbox3", "Textbox4"), _
Array("B", "C", "D", "E")
End Sub
In a regular module:
'### This code goes in a regular VBA Module ###
' sht = sheet hostong the controls
' lbName = name of listbox on that sheet
' arrTbName = array of textbox names
' arrColAddr = array of column source addresses, matching order
' of the values in arrTbName
Sub Handle_Change(sht As Worksheet, lbName, arrTbName, arrColAddr)
Dim idx As Long, lb As msforms.ListBox, i As Long
Set lb = sht.Shapes(lbName).OLEFormat.Object.Object
idx = lb.ListIndex
If idx <> -1 Then
'loop through the textbox and column arrays, setting each pair
For i = LBound(arrTbName) To UBound(arrTbName)
sht.Shapes(arrTbName(i)).OLEFormat.Object.Object.Text = _
sht.Range(arrColAddr(i) & idx + 1).Value
Next i
End If
End Sub
Sample file: https://dl.dropboxusercontent.com/u/15526711/Listboxes.xlsm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.