+ Reply to Thread
Results 1 to 23 of 23

VBA to filter by user specified criteria

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    VBA to filter by user specified criteria

    Morning,

    Is it possible to write code so that it filters data specifically on what the user wants to filter by.

    For example.

    If the user specified filter by column 19, GBP?
    Last edited by kenadams378; 08-30-2012 at 07:05 AM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    How would the user be specifying the criteria?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    by entering a column number and then criteria

    For example

    column = 19
    criteria = GBP.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    Where would the criteria be specified?

  5. #5
    Registered User
    Join Date
    08-19-2012
    Location
    Cambodia
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: VBA to filter by user specified criteria

    It is possible. You may read this page to get the idea:http://www.worldbestlearningcenter.c...ta-example.htm

  6. #6
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    On a front sheet called 'start'

    Cell A1, A2

  7. #7
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    Try this
    Dim lrow As Long
        
        lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
        Worksheets("Sheet1").Range("$A$1:$Z$" & lrow).AutoFilter Field:=Worksheets("Start").Range("A1").Value, Criteria1:=Worksheets("Start").Range("A2").Value

  8. #8
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    Thanks Arlu,

    The following code
    .AutoFilter Field:=Worksheets("Start").Range("O13").Value, Criteria1:=Worksheets("Start").Range("O14").Value, Criteria2:=Worksheets("Start").Range("O15").Value
                .Copy ThisWorkbook.Worksheets("S29 Data").Range("A1")
    Can you check if this is okay, i get an error message to say subscript out of range

  9. #9
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    Subscript out of range has everything to do with the sheet names.

    Just check that your sheet names are named properly and matching with the code in terms of case and spelling.

  10. #10
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    checked and names are okay, the only thing i can think of is that does it need to be Auto Filter Column rather than field?!

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    No the syntax says field.

    You can try recording the code with some temp fields and then change them accordingly to your start sheet fields.

  12. #12
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    Still not working, i have attached the workbook here, can you have a look for me, the filter is applied as part of step 1?
    Attached Files Attached Files

  13. #13
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    Have you been able to look at my attachment Arlu?

  14. #14
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    In the following line of code i have the error 'object required'

    Any ideas on what the issue is;

                .AutoFilter Field:=Worksheets("Start").Range("N13").Value, Criteria1:=Worksheets("Start").Range("N14").Value, Criteria2:=Worksheets("Start").Range("N15").Value

  15. #15
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    What is the code before that line?

  16. #16
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

     With wb.Worksheets("Sheet1")
           lrow = .Range("A" & .Rows.Count).End(xlUp).Row
            With .Range("A1:DC" & lrow)

  17. #17
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

     With wb.Worksheets("Sheet1")
           lrow = .Range("A" & .Rows.Count).End(xlUp).Row
            With .Range("A1:DC" & lrow)
    What is wb declared as in the dim statements?

  18. #18
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    It is declared as Workbook?

    Dim wb As Workbook

  19. #19
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    Can you post your whole code here? It needs a lil change.

  20. #20
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    Sub Bevel1()
        Dim lrow As Long
        Dim FName As Variant
        Dim wb As Workbook
        Dim vSheets
        Dim vSheet
        Dim lRows As Long
    
        'Turn off settings that slow down the macro process'
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        End With
    
        vSheets = Array("Morca", "Moeca")
    
        ChDir "\\Via.novonet\dfs\LIFE\F\INVReporting\CONFIDENTIAL\Inv_Reporting_2012\"
    
        For Each vSheet In vSheets
            'Prompt user for most recent MOECA/MORCA Liquidity Data filepath'
    
            FName = Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", _
                                                Title:="Please open the most recent " & vSheet & " Data file")
    
            If FName = "False" Then
                MsgBox "You have not selected a file."
                Exit Sub
            End If
    
            Set wb = Workbooks.Open(Filename:=FName, UpdateLinks:=False, ReadOnly:=True)
    
            'Using the filepath, select original data sheet and clear any old data from the sheet'
            'Select the range of data from the liquidity data and paste this into the original data tab'
            'Repeat using array'
            
            ThisWorkbook.Worksheets("Original " & vSheet).Cells.ClearContents
            With wb.Worksheets("Sheet1")
                lrow = .Range("A" & .Rows.Count).End(xlUp).Row
                .Range("A1:P" & lrow).Copy ThisWorkbook.Worksheets("Original " & vSheet).Range("A1")
            End With
            wb.Close False
        Next vSheet
    
            'Prompt user for most recent S29 file'
    
        FName = Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", _
                                            Title:="Please open the most recent S29 Data file")
    
        If FName = "False" Then
            MsgBox "You have not selected an S29 file."
            Exit Sub
        End If
    
            'Once file is open, clear data from S29 Data sheet and copy range from FName, filter using specfic criteria on
            'Field 19 (Stock) and paste to S29 Data sheet in Cell A1
    
        Set wb = Workbooks.Open(Filename:=FName, UpdateLinks:=False, ReadOnly:=True)
    
        ThisWorkbook.Worksheets("S29 Data").Cells.ClearContents
        
        With wb.Worksheets("Sheet1")
           lrow = .Range("A" & .Rows.Count).End(xlUp).Row
            With .Range("A1:DC" & lrow)
                .AutoFilter Field:=19, Criteria1:="GBPMORCA", Operator:=xlOr, Criteria2:="GBPMOECA"
                .Copy ThisWorkbook.Worksheets("S29 Data").Range("A1")
            End With
            wb.Close False
             
            'Add username, date & time of the last time the macro was used'
    
    With Sheets("Start")
        .Cells(31, 2) = Now
        .Cells(32, 2) = CreateObject("WScript.Network").UserName
        End With
        
    End With
    End Sub

  21. #21
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    Try this
    Option Explicit
    
    Sub Bevel1()
        Dim lrow As Long
        Dim FName As Variant
        Dim wb As Variant
        Dim vSheets
        Dim vSheet
        Dim lRows As Long
    
        'Turn off settings that slow down the macro process'
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
        End With
    
        vSheets = Array("Morca", "Moeca")
    
        ChDir "\\Via.novonet\dfs\LIFE\F\INVReporting\CONFIDENTIAL\Inv_Reporting_2012\"
    
        For Each vSheet In vSheets
            'Prompt user for most recent MOECA/MORCA Liquidity Data filepath'
    
            FName = Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", _
                                                Title:="Please open the most recent " & vSheet & " Data file")
    
            If FName = "False" Then
                MsgBox "You have not selected a file."
                Exit Sub
            End If
    
            Set wb = Workbooks.Open(Filename:=FName, UpdateLinks:=False, ReadOnly:=True)
    
            'Using the filepath, select original data sheet and clear any old data from the sheet'
            'Select the range of data from the liquidity data and paste this into the original data tab'
            'Repeat using array'
            
            ThisWorkbook.Worksheets("Original " & vSheet).Cells.ClearContents
            With Workbooks(wb).Worksheets("Sheet1")
                lrow = .Range("A" & .Rows.Count).End(xlUp).Row
                .Range("A1:P" & lrow).Copy ThisWorkbook.Worksheets("Original " & vSheet).Range("A1")
            End With
            Workbooks(wb).Close False
        Next vSheet
    
            'Prompt user for most recent S29 file'
    
        FName = Application.GetOpenFilename(FileFilter:="All files (*.*), *.*", _
                                            Title:="Please open the most recent S29 Data file")
    
        If FName = "False" Then
            MsgBox "You have not selected an S29 file."
            Exit Sub
        End If
    
            'Once file is open, clear data from S29 Data sheet and copy range from FName, filter using specfic criteria on
            'Field 19 (Stock) and paste to S29 Data sheet in Cell A1
    
        Set wb = Workbooks.Open(Filename:=FName, UpdateLinks:=False, ReadOnly:=True)
    
        ThisWorkbook.Worksheets("S29 Data").Cells.ClearContents
        
        With Workbooks(wb).Worksheets("Sheet1")
           lrow = .Range("A" & .Rows.Count).End(xlUp).Row
            With .Range("A1:DC" & lrow)
                .AutoFilter Field:=19, Criteria1:="GBPMORCA", Operator:=xlOr, Criteria2:="GBPMOECA"
                .Copy ThisWorkbook.Worksheets("S29 Data").Range("A1")
            End With
            Workbooks(wb).Close False
             
            'Add username, date & time of the last time the macro was used'
    
    With Sheets("Start")
        .Cells(31, 2) = Now
        .Cells(32, 2) = CreateObject("WScript.Network").UserName
        End With
        
    End With
    
    With Application
            .ScreenUpdating = True
            .DisplayAlerts = True
        End With
    End Sub

  22. #22
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA to filter by user specified criteria

    Thanks but i have an error on the same line

    Subscript out of range

    The only sheet this effects is the start sheet and that is named the same as the macro

  23. #23
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: VBA to filter by user specified criteria

    Is it this line that is affected? -
    With Workbooks(wb).Worksheets("Sheet1")
    Does your file have a Sheet1? The file that you are opening through the macro?

+ Reply to 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