Excel

Listbox filled with matching names in sheet two from partial searchstring in sheet1

Ease of Use

Intermediate

Version tested with

2002, 2003 

Submitted by:

Charlize

Description:

Populate a listbox by entering a partial searchvalue in column A on sheet1 after hitting enter. Listbox will show matching names in sheet2. 

Discussion:

Maybe usefull for a helpdesk or Debit - Credit controller. Taking an incoming call and need a record for every call. Type in partial name and you can see all the matching results in the listbox with adress and city (maybe a 4th column to see if he has paid for the helpdesk). 

Code:

instructions for use

			

--- place the text between --- In the code For sheet1 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 1 Then UserForm1.Show End If End Sub --- End of place this In sheet1 --- place the text between --- In a module Option Explicit Function xlLastRow(Optional WorksheetName As String) As Long 'Check for optional worksheetname else use activesheet If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name End If ' find the last populated row in a worksheet With Worksheets(WorksheetName) xlLastRow = .Cells.Find("*", .Cells(1), xlFormulas, _ xlWhole, xlByRows, xlPrevious).Row End With End Function --- End of place this In a module --- place the text between --- In the code For a userform Option Explicit Option Compare Text Private Sub UserForm_Activate() Dim Cell As Range 'defining your searcharea Dim Row_Counter As Integer 'rowno Dim Pos As Integer 'rowno in array Dim MyList() As String 'the array Dim No_Pos As Integer 'total rows in aray Dim Current_pos As Integer 'position in sheet1 Dim Search_name As String 'searchvalue in sheet1 Dim Real_last_row As Integer 'last row in sheet2 'xllastrow is a function Real_last_row = xlLastRow("Sheet2") 'last row in sheet2 Current_pos = ActiveCell.Row 'after hitting enter Current_pos = Current_pos - 1 'before hitting enter 'putting the value of the cell in a variable before you hitted enter Search_name = Worksheets("Sheet1").Range("A" & Current_pos) 'Searching in sheet2 for matches with searchstring 'Needed for creating an array with unknown number of rows For Each Cell In Worksheets("Sheet2").Range("A2:A" & Real_last_row) If Cell Like "*" & Search_name & "*" Then 'If a value matches with the searchstring, number of rows of array is + 1 No_Pos = No_Pos + 1 End If Next Cell Row_Counter = 2 'start of the nameslist Pos = 0 'rowno in array, beginning with zero ReDim Preserve MyList(No_Pos, 3) 'Redimming of array with total no of rows and 3 columns 'if value isn't in searchstring then rowno is rowno + 1 'if a match is found the array is filled and the rowno in array is + 1 For Each Cell In Worksheets("Sheet2").Range("A2:A" & Real_last_row) If Cell Like "*" & Search_name & "*" Then MyList(Pos, 0) = Worksheets("Sheet2").Range("A" & Row_Counter) MyList(Pos, 1) = Worksheets("Sheet2").Range("B" & Row_Counter) MyList(Pos, 2) = Worksheets("sheet2").Range("C" & Row_Counter) Pos = Pos + 1 Row_Counter = Row_Counter + 1 Else Row_Counter = Row_Counter + 1 End If Next Cell Application.ShowToolTips = True With ListBox1 .ColumnCount = 3 'no of columns (0,1,2) .ColumnWidths = "3 cm;3 cm;3 cm" 'widths of columns .ControlTipText = "Possible matches ..." 'tiptext .ListStyle = fmListStylePlain .SpecialEffect = fmSpecialEffectFlat End With ListBox1.List = MyList 'define the list of listbox1 End Sub --- End of place this In the userform_activate

How to use:

  1. hit alt+f11 for the vbe
  2. copy each section to the place indicated between --- without those ---
  3. right mouse click on sheet1, choose 1st item in pop-up menu and past the code that you've copied to the clipboard
  4. to insert module, right mouse click on project on left side of screen, choose insert module
  5. to insert code for module, right mouse click on module, choose 1st item in pop-up menu and past the code that you've copied to the clipboard
  6. if no userform present, insert one - right mouse click, choose insert form
  7. to insert code for userform - right mouse click on userform, choose 1st item in pop-up menu and past the code that you've copied to the clipboard
  8. go back to excel and save your workbook
 

Test the code:

  1. add some names in sheet2 - column A = name - column B = adress - column C = city
  2. in sheet1 type a partial searchstring in column A and hit enter
  3. a listbox should popup with the result in it
  4. note : option compare text in userformcode means that "S" and "s" are equal. So if you type in s you'll get Smith, Jones, ... so every name with an s in it.
 

Sample File:

VBA-Express - Address Validation-Listbox-v2.zip 17.95KB 

Approved by mdmackillop


This entry has been viewed 425 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express