?
Solved

When Excel VBA uses AutoFilter, extra rows are hidden after filter

Posted on 2010-09-08
3
Medium Priority
?
247 Views
Last Modified: 2012-05-10
I've got VBA code that filters a portion of data (on sheet tab "Equipment Worksheet") after selections are made on a different sheet ("SW Worksheet, cells H251:H254). Everything works great EXCEPT that the filtering, which is supposed to end at row 164 on "Equipment Worksheet", actually hides everything through row 192, and I can't figure out how to only make it hide through row 164.

The file is attached: a selection has already been made on the "SW Worksheet;" to run the code, click the gray button at cell D68 on "Equipment Worksheet."

I appreciate any ideas!
Sub PickCoffee()
    Application.ScreenUpdating = False
    Dim br As String                'Brew Prep Area
    Dim barLen As String            'Bar Length
    Dim vol As String               'Volume
    Dim s As String                 'sink
    
    Sheets("SW Worksheet").Select
    'set barLen
    If IsEmpty(Range("H252")) Then
        MsgBox "Please select coffee bar length."
        Range("H252").Select
        Exit Sub
    Else
        barLen = Range("H252").Value
    End If
    
    'set s
    If IsEmpty(Range("H254")) Then
        s = "x"                     'no sink criteria will be used in filter
    ElseIf Range("H254").Value = "None" Then
        s = ""
    Else
        s = Range("H254").Value
    End If
    
    'set br
    If IsEmpty(Range("H251")) Then
        br = "x"                    'no brew prep criteria will be used in filter
    ElseIf Range("H251") = "back wall" Then
        br = ""
    Else
        br = "Y"
    End If
    
    'set vol
    If IsEmpty(Range("H253")) Then
        vol = "x"
    Else
        Select Case Range("H253").Value
            Case "Low"
                vol = "LV"
            Case "Med/High"
                vol = "MV/HV"
            Case "High"
                vol = "HV"
            Case "Extremely High"
                vol = "EV"
            Case Else
                MsgBox "Vol is " & Range("H253").Value
        End Select
    End If
    
    'If AutoFilter already on and filtered, then unfilter
    Worksheets("Equipment Worksheet").Activate
    If ActiveSheet.FilterMode Then
        Range("J70").Select
        ActiveSheet.ShowAllData
    Else
        Range("G69:R164").Select
        Selection.AutoFilter
        Range("G70").Select
    End If
    
    'now filter data with given data
    'bar length
    Selection.AutoFilter Field:=7, Criteria1:=barLen
    
    'brew prep area
    If br = "x" Then
        Selection.AutoFilter Field:=12
    Else
        'ActiveSheet.Range("$G$69:$R$164").AutoFilter Field:=12, Criteria1:=br
        Selection.AutoFilter Field:=12, Criteria1:=br
    End If
    
    'volume
    If vol = "x" Then
        Selection.AutoFilter Field:=8
    Else
        Selection.AutoFilter Field:=8, Criteria1:=vol
    End If
    
    'sink
    If s = "x" Then
        Selection.AutoFilter Field:=9
    Else
        Selection.AutoFilter Field:=9, Criteria1:=s
    End If
    
    ActiveSheet.Range("D70").Select
    Application.ScreenUpdating = True
    MsgBox "Based on choices made in cells H251 - H254 on the SW Worksheet," & _
        vbCrLf & "your options are now visible here." & _
        vbCrLf & vbCrLf _
        & "If no options appear here, change your choices on the SW Worksheet."
        
End Sub

Open in new window

SOW-for-EE.xls
0
Comment
Question by:nbozzy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 33631116
Make row 165 blank. :)
0
 

Author Comment

by:nbozzy
ID: 33631225
Shoot me! You know, rorya, I actually did have it blank at one point, but I changed my code since then. Silly me.....thanks so much!
0
 

Author Closing Comment

by:nbozzy
ID: 33631234
Glad you were there for me!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question