Trying to use some code I found on a blog on how to limit a ListBox.
I have multiple worksheets and I would like to run this user form with any of the sheets active and NOT the one containing the data for the form....
1- the list box does NOT get populated with values from sheet 1 if it is not active
2- I get a type mismatch on the change event.....
I have multiple worksheets and I would like to run this user form with any of the sheets active and NOT the one containing the data for the form....
Code:
Private Sub UserForm_Initialize()
With Sheet1
.Cells.Sort Key1:=.Range("B2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
'[URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?qid=1438167&page=1[/URL]
Select Case .[B1].CurrentRegion.Rows.Count
Case 2
Me.lbxCustomers.RowSource = .[B2].address
Case Is > 2
Me.lbxCustomers.RowSource = .Range(.[B2], .[B2].End(xlDown)).address
End Select
End With
End Sub
Private Sub tbxFind_Change()
Dim vList As Variant
'Read the original list
With Sheet1
vList = .Range(.[B2], .Cells(Rows.Count, 2).End(xlUp)).value
End With
'Convert it to a 1D array
vList = Application.Transpose(vList)
'Filter it
vList = Filter(SourceArray:=vList, _
Match:=tbxFind.value, _
Include:=True, _
Compare:=vbTextCompare)
'Send it to the listbox
lbxCustomers.List = vList
If lbxCustomers.ListCount = 0 Then
'
Customers.txtName.Text = Me.tbxFind.value
Unload Me
Customers.Show
End If
End Sub
2- I get a type mismatch on the change event.....