+ Reply to Thread
Results 1 to 2 of 2

Search needed that populates a defined array.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-21-2010
    Location
    Tulsa, OK
    MS-Off Ver
    Excel 2007
    Posts
    1

    Question Search needed that populates a defined array.

    I have the following bit of code (gathered from various resources) that almost fulfills what I need, but, is missing a crucial 'search' element.

    Private Sub CommandButton1_Click()
    
    ' Clear Listbox of last search results
    Me.ListBox1.Clear
    
    Dim lb As MSForms.ListBox
    Dim srchArray() As Variant
    Dim lrw As Long, lcol As Long
    Dim rngTarget As Range
    
    SearchString = InputBox("Enter any of the following criteria to complete your search", "Search")
    
    Set rngTarget = Worksheets("Sheet1").Range("b5:h900")
    ReDim Preserve srchArray(1 To rngTarget.Rows.count, 1 To rngTarget.Columns.count)
    
    With rngTarget
    
        For lcol = 1 To .Columns.count
            For lrw = 1 To .Rows.count
    
                         ' Do search here?  Add results to array srchArray....
                         srchArray(lrw, lcol) = rngTarget.Cells(lrw, lcol)
    
                Next lrw
            Next lcol
    
    End With
    
    Set lb = Me.ListBox1
    With lb
        .ColumnCount = 7
        .ColumnWidths = "100;100;30;300;30;80;100"
        .List = srchArray
    End With
    End Sub
    Which works, sorta, just not completely.

    A little backstory: months ago, the data required to be returned to the listbox was just one column, so the search that I was using...

    Private Sub CommandButton1_Click()
    Me.ListBox1.Clear
        SearchString = InputBox("Enter any of the following criteria to complete your search, "Search")
            For Each d In Range("b6:g900")
            If InStr(LCase(CStr(d)), LCase(SearchString)) Then
                With ListBox1
                    .AddItem Cells(d.Row, 2) & " - " & Cells(d.row, 3) 'etc.
                End With
            End If
            Next d
     End Sub
    worked great until more columns were added and the corresponding data had to be returned as well. Which doesn't mean the above didn't still work, it did, I just had to concatenate the other columns to each other. But, the problem was that none of the columns lined up properly, nor could I get the column headers like I wanted. So, I switched to the first method using an array to build my listbox - giving me greater control of the column widths (where I had none) and the headers as well.

    But, I can't quite figure out how to iterate through the defined range, search for the desired text and then add that result to the array so that I can build my listbox.

    I've tried several iterations of adding the search function to the second bit of code, but, no luck; I either get NO results, or a result containing everything within defined range.

    Any help is appreciated. My google-fu is quite lacking today.

    Thanks in advance. If you need more information, glad to provide.

    J

  2. #2
    Registered User
    Join Date
    04-06-2010
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    42

    Re: Search needed that populates a defined array.

    Welcome to the board. There's various ways of managing this - within the VBE Help look for the "Find Method" topic. Post again if your having trouble with this.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1