Somebody on here showed me how to do a search function and show results in a listbox once upon a time.
The initial sheet is called "IOW DB" and has like a refID, NatID, Name, Location, InterviewDate, TodayDate, DateOfContact, Processor, InfoNeeded, ReceivedDate, InfoProvided, Status, InfoMissing i.e. 13 columns.
My search script searches for all instances of "NatID" and copies all of them to a temporary "SearchResults" sheet. But it copies all 13 columns.
So when it shows the results in my list box, it shows all 13 columns of data and is obviously too big for the list box.
I'd like to restrict the data in the list box to just 4 columns. The refID (column B), InterviewDate (column F), DateOfContact (column D) and Status (column L).
Is there a way I can do this?
This is my code:
For this piece of code:Code:Private Sub CommandButton1_Click() If Me.NatID.Value = "" Then MsgBox "You must enter a NatID to search" Else Dim LR As Long Dim FindString As String LR = Sheets("IOW DB").Range("A" & Rows.Count).End(xlUp).Row FindString = NatID.Value If Trim(FindString) <> "" Then With Sheets("IOW DB").Range("A2:A" & LR) Set Rng = .Find(What:=FindString, _ After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then Application.Goto Rng.Offset(0, 0), True ' Carry out search and populate list box 'define variables Dim varSourceRow As Integer Dim varTargetRow As Integer Dim varRowCount As Integer Dim varColumn As Integer Dim varReferenceNumber As String Dim ws As Object Dim WStemp As Object Dim FirstCell As Range Dim LastCell As Range Dim varListViewRange As Range Set ws = Sheets("IOW DB") Set WStemp = Sheets("SearchResults") 'cleardown previous search WStemp.Range("A2:M5000").Value = "" 'count the total number of records varRowCount = Sheets("IOW DB").Cells(50000, 1).End(xlUp).Row 'define search string varReferenceNumber = Me.NatID.Value 'reset loop counters varTargetRow = 2 For varSourceRow = 2 To varRowCount If ws.Cells(varSourceRow, 1).Value = varReferenceNumber Then For varColumn = 1 To 13 WStemp.Cells(varTargetRow, varColumn).Value = ws.Cells(varSourceRow, varColumn).Value Next varColumn varTargetRow = varTargetRow + 1 End If Next varSourceRow 'Cancel if there are no results found If WStemp.Cells(2, 1).Value = "" Then Exit Sub End If 'Count the number of results varLastRow = Sheets("SearchResults").Cells(50000, 1).End(xlUp).Row 'Define the range for the listbox With WStemp .Columns("A:M").EntireColumn.AutoFit Set FirstCell = .Cells(2, 1) Set LastCell = .Cells(varLastRow, 13) Set varListViewRange = .Range(FirstCell, LastCell) End With 'Link the listbox to the range and adjust column widths With Me.IOWResults .RowSource = varListViewRange.Address(external:=True) ColWidth = "" For c = 1 To 13 ColWidth = "0" Next c .ColumnWidths = ColWidth .ListIndex = 0 End With Else MsgBox "No Results for that NatID, '" & Me.NatID.Value & "'" End If End With End If End If End Sub
I did try to doCode:'Define the range for the listbox With WStemp .Columns("A:M").EntireColumn.AutoFit
But it was having none of it :-(Code:'Define the range for the listbox With WStemp .Columns("A:A,D:D,F:F,L:L").EntireColumn.AutoFit
Thanks guys
Bookmarks