Results 1 to 2 of 2

Autofilter Problems

Threaded View

  1. #1
    Registered User
    Join Date
    12-05-2008
    Location
    south west england
    Posts
    1

    Autofilter Problems

    I'm trying to set up an autofilter macro that will filter out results from multiple sheets using criteria that is set up on an independant worksheet at the begining of the workbook and return the information to another sheet. I'm using the following code but I can't seem to get it to work. Any help would be appreciated
    Sub Sort_data()
    Dim MaxResults As Integer, MyCol As Integer, ResultsRng As String
    Dim MyRow As Integer, LastDataRow As Integer, DataRng As String
    Dim CritRow As Integer, CritRng As String, RightCol As Integer
    Dim TopRow As Integer, BottomRow As Integer, LeftCol As Integer
    
    
    LastDataRow = Worksheets("SHEET2").Range("B100").Value
    
    DataRng = "A1:D1" ' range of column headers for Data table
    CritRng = "sheet1!A1:D1" ' range of cells for Criteria table
    ResultsRng = "Sheet1!A6:D6" ' range of headers for Results table
    MaxResults = 1000 ' any value higher than the number of possible results
    
    
    TopRow = Range(DataRng).Row
    LeftCol = Range(DataRng).Column
    RightCol = LeftCol + Range(DataRng).Columns.Count - 1
    DataRng = Range(Cells(TopRow, LeftCol), Cells(LastDataRow, RightCol)).Address
    
    TopRow = Range(ResultsRng).Row
    LeftCol = Range(ResultsRng).Column
    RightCol = LeftCol + Range(ResultsRng).Columns.Count - 1
    ResultsRng = Range(Cells(TopRow + 1, LeftCol), Cells(MaxResults, RightCol)).Address
    Range(ResultsRng).ClearContents ' clear any previous results but not headers
    ResultsRng = Range(Cells(TopRow, LeftCol), Cells(MaxResults, RightCol)).Address
    
    
    TopRow = Range(CritRng).Row
    BottomRow = TopRow + Range(CritRng).Rows.Count - 1
    LeftCol = Range(CritRng).Column
    RightCol = LeftCol + Range(CritRng).Columns.Count - 1
    CritRow = 0
    
    For MyRow = TopRow + 1 To BottomRow
    For MyCol = LeftCol To RightCol
    If Cells(MyRow, MyCol).Value <> "" Then CritRow = MyRow
    Next
    Next
    
    If CritRow = 0 Then
    MsgBox "No Criteria detected", "MeadInKent"
    Else
    CritRng = Range(Cells(TopRow, LeftCol), Cells(CritRow, RightCol)).Address
    Debug.Print "DataRng, CritRng, ResultsRng: ", DataRng, CritRng, ResultsRng
    
    Worksheets("Data").Range(DataRng).AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=Range(CritRng), CopyToRange:=Range(ResultsRng), _
    Unique:=False
    End If
    Range("A5").Select
    
    
    End Sub
    Last edited by Leith Ross; 12-05-2008 at 10:28 PM. Reason: Added Code Tags

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