Private Sub UserForm_Initialize()
Dim aRow As Long
Dim LastRow As Long
LastRow = ActiveWorkbook.Worksheets("MySheet").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For aRow = 2 To LastRow
Me.lboxClientes.AddItem Sheet1.Range("G" & aRow)
Next
End Sub
How can I make the code to load data into user form's listbox from columns D to column Z. My data rows start from row 17 in "MySheet".
Private Sub UserForm_Initialize()
Dim LastRow As Long
Dim wksMySheet As Worksheet
Dim aMyArray As Variant
Set wksMySheet = ActiveWorkbook.Worksheets("MySheet")
LastRow = wksMySheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
aMyArray = wksMySheet.Range("D17:Z" & LastRow)
With Me.lboxClientes
.ColumnCount = 26 - 4
.List() = aMyArray
End With
End Sub
Kevin
Last edited by Becks on 20 May 2011, 10:26, edited 1 time in total.
Thanks for the help. I have a text box which filters the list box with the value in the text box. But this text box searches values only from column G but not the other columns.
Private Sub txtSearchItem_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim aRow As Long
Dim LastRow As Long
Dim WS As Worksheet
Set WS = ActiveWorkbook.Worksheets("MySheet")
If Me.txtSearchItem <> "" Then
Me.lboxClientes.Clear
With Me.lboxClientes
LastRow = WS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For aRow = 2 To LastRow
If InStr(UCase(WS.Range("G" & aRow).Value), UCase(Me.txtSearchItem)) <> 0 Then
.AddItem WS.Range("G" & aRow).Value
End If
Next
End With
End If
End Sub
Suppose If I have apple in column B, the code does not filter the list box if I write apple. But it filters the values that I write from column G. How could I make the code to search text from the appropriate column and filter the list box.
Any help on this would be kindly appreciated. Thanks in advance.
Private Sub txtSearchItem_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim aRow As Long
Dim iCol As Integer
Dim LastRow As Long
Dim wksMySheet As Worksheet
Dim aMyArray()
Set wksMySheet = ActiveWorkbook.Worksheets("MySheet")
If Me.txtSearchItem <> "" Then
Me.lboxClientes.Clear
With Me.lboxClientes
LastRow = wksMySheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
ReDim aMyArray(17 To LastRow, 4 To 26)
For aRow = 17 To LastRow
If InStr(UCase(WS.Range(aMyArray & aRow).Value), UCase(Me.txtSearchItem)) <> 0 Then
.AddItem WS.Range(aMyArray & aRow).Value
For iCol = 4 To 26
aMyArray(aRow, iCol) = wksMySheet.Cells(aRow, iCol).Value
Next iCol
Next aRow
With Me.lboxClientes
.ColumnCount = 26 - 4
.List() = aMyArray
End With
End Sub
Private Sub txtSearchItem_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim aRow As Long
Dim iCol As Integer
Dim LastRow As Long
Dim wksMySheet As Worksheet
Dim aMyArray()
Dim rngArray As Range
Dim rngFound As Range
If Len(Me.txtSearchItem.Value) = 0 Then Exit Sub
Set wksMySheet = ActiveWorkbook.Worksheets("MySheet")
LastRow = wksMySheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For aRow = 17 To LastRow
Set rngFound = wksMySheet.Range("D" & aRow).Resize(1, 22).Find(What:=Me.txtSearchItem.Value, LookAt:=xlPart, MatchCase:=False)
If Not (rngFound Is Nothing) Then
Set rngFound = wksMySheet.Range("D" & aRow).Resize(1, 22)
If rngArray Is Nothing Then
Set rngArray = rngFound
Else
Set rngArray = Application.Union(rngArray, rngFound)
End If
End If
Next aRow
aMyArray = rngArray
With Me.lboxClientes
.Clear
.List() = aMyArray
End With
End Sub
Private Sub txtSearchItem_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim aRow As Long
Dim iCol As Integer
Dim LastRow As Long
Dim wksMySheet As Worksheet
Dim aMyArray()
Dim rngArray As Range
Dim rngFound As Range
Set wksMySheet = ActiveWorkbook.Worksheets("MySheet")
LastRow = wksMySheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
If Len(Me.txtSearchItem.Value) = 0 Then
aMyArray = wksMySheet.Range("D17:Z" & LastRow)
With Me.lboxClientes
.Clear
.List() = aMyArray
End With
Else
For aRow = 17 To LastRow
Set rngFound = wksMySheet.Range("D" & aRow).Resize(1, 22).Find(What:=Me.txtSearchItem.Value, LookAt:=xlPart, MatchCase:=False)
If Not (rngFound Is Nothing) Then
Set rngFound = wksMySheet.Range("D" & aRow).Resize(1, 22)
If rngArray Is Nothing Then
Set rngArray = rngFound
Else
Set rngArray = Application.Union(rngArray, rngFound)
End If
End If
Next aRow
aMyArray = rngArray
With Me.lboxClientes
.Clear
.List() = aMyArray
End With
End If
End Sub