Closed Thread
Results 1 to 3 of 3

Search Range, List Results in MultiColumn ListBox

Hybrid View

  1. #1
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The database form example does this, get it here

    http://www.excel-it.com/vba_examples.htm
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    This uses AdvancedFilter to copy the records matching the search term to another sheet. It uses an InputBox to get the search term from the user.
    Sub AdvancedFilterMove()
        Dim dataRange As Range
        Dim keyCol As Range, keyValue As Variant
        Dim destinationSheet As Worksheet
        Dim destinationRange As Range
        Dim critRange As Range
        
        keyValue = Application.InputBox("Enter your search term", Type:=2)
        If keyValue = "False" Then Exit Sub: Rem cancel pressed
        
        With ThisWorkbook.Sheets("SourceSheet"): Rem adjust
            Set keyCol = Range("A:A"): Rem adjust
            Set dataRange = Range(.Cells(.Rows.Count, 1).End(xlUp), .Cells(1, .Columns.Count).End(xlToLeft))
        End With
        
        Set destinationSheet = ThisWorkbook.Sheets("destinationSheet")
        Set destinationRange = destinationSheet.Range("a1")
              
        Rem set critrange
            With destinationRange.Parent.UsedRange
                Set critRange = .Parent.Cells(1, .Column + .Columns.Count + 1)
            End With
            If critRange.Column < destinationRange.Column + dataRange.Columns.Count Then
                Set critRange = critRange.Parent.Cells(1, destinationRange.Column + dataRange.Columns.Count)
            End If
            
            Rem write criteria
            Set critRange = critRange.Resize(2, 1)
            critRange.Range("A1").Value = keyCol.Range("a1").Value
            critRange.Range("A2").FormulaR1C1 = "'=" & keyValue
        Rem filter + move
            destinationRange.Resize(, dataRange.Columns.Count).EntireColumn.ClearContents
                
            dataRange.AdvancedFilter Action:=xlFilterCopy, _
                CriteriaRange:=critRange, CopyToRange:=destinationRange, Unique:=False
        Rem clean-up
            critRange.EntireColumn.Delete
    End Sub
    (Removing the "'=" from this line will find the records that start with the search term, leaving it in will find exact matches only.)
    critRange.Range("A2").FormulaR1C1 = "'=" & keyValue
    Last edited by mikerickson; 07-04-2008 at 11:47 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

Closed Thread

Thread Information

Users Browsing this Thread

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

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