Closed Thread
Results 1 to 8 of 8

Hide rows based on userform checkbox

Hybrid View

rodrignj Hide rows based on userform... 02-25-2013, 10:37 AM
AlphaFrog Re: Hide rows based on... 02-25-2013, 11:15 AM
rodrignj Re: Hide rows based on... 02-25-2013, 11:46 AM
AlphaFrog Re: Hide rows based on... 02-25-2013, 12:58 PM
rodrignj Re: Hide rows based on... 02-25-2013, 01:06 PM
meabrams Re: Hide rows based on... 01-21-2015, 05:35 AM
meabrams Re: Hide rows based on... 01-21-2015, 05:36 AM
Fotis1991 Re: Hide rows based on... 01-21-2015, 05:38 AM
  1. #1
    Registered User
    Join Date
    12-11-2012
    Location
    cincinnati, ohio
    MS-Off Ver
    Excel 2010
    Posts
    43

    Hide rows based on userform checkbox

    Good morning everyone,

    I have a macro that I want to sort data. A button in my workbook calls a small userform with 10 checkboxes. The user should pick those categories that he wants to review and click sort. The result I want is for only the categories he chose to be displayed but I am getting an all or nothing result out of the attached macro. Below is that macro that supports the form/button to sort the categories. I feel like the answer is right under my nose but I am just not seeing it! Any help you could offer would be greatly appreciated.

    Thanks!

    Private Sub cmdSort_Click()
        
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        
        If chkFE = True Then
            For Each cell In Range("BC4:BC" & LastRow)
                If UCase(cell.Value) <> "Fire Extinguishers" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
        
        If chkChem = True Then
            For Each cell In Range("BD4:BD" & LastRow)
                If UCase(cell.Value) <> "Chem" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkFL = True Then
            For Each cell In Range("BE4:BE" & LastRow)
                If UCase(cell.Value) <> "FL" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkElec = True Then
            For Each cell In Range("BF4:BF" & LastRow)
                If UCase(cell.Value) <> "Elec" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkFP = True Then
            For Each cell In Range("BG4:BG" & LastRow)
                If UCase(cell.Value) <> "FP" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkLift = True Then
            For Each cell In Range("BH4:BH" & LastRow)
                If UCase(cell.Value) <> "Lift" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkPPE = True Then
            For Each cell In Range("BI4:BI" & LastRow)
                If UCase(cell.Value) <> "PPE" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkPS = True Then
            For Each cell In Range("BJ4:BJ" & LastRow)
                If UCase(cell.Value) <> "PS" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkSTF = True Then
            For Each cell In Range("BK4:BK" & LastRow)
                If UCase(cell.Value) <> "STF" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
            
        If chkErgonomics = True Then
            For Each cell In Range("BL4:BL" & LastRow)
                If UCase(cell.Value) <> "Ergonomics" Then
                cell.EntireRow.Hidden = True
                End If
            Next
    
        End If
        
    
        
        Unload frmSort
    
    End Sub

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Hide rows based on userform checkbox

    You are comparing UPPER CASE to Mixed Case. They would never equal.

    UCase(cell.Value) <> "Fire Extinguishers"


    You may also want to use the Autofilter feature instead of looping.

  3. #3
    Registered User
    Join Date
    12-11-2012
    Location
    cincinnati, ohio
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Hide rows based on userform checkbox

    @AlphaFrog, thanks for the help. Eliminating UCase from the macro helped with this. The problem that I have now is, the macro works fine if I select only one category out of the UserForm, however if I select more than 1 it will eliminate all the rows except those rows where the chosen categories overlap. I want the macro to show all of the categories that I choose. I am extremely new to vba, but I looked up how to use the Autofilter and I am not really sure how I could apply this to my situation. I understand how I could provide a static sort based on a column of cells but I would need a way to say "If the 'Fire Extinguishers' checkbox is checked, then review column BC and hide all that don't display 'Fire Extinguishers'" furthermore I would need it to say "If I choose 'Fire Extinguishers' and 'Ergonomics' review their respective columns and hide all rows where one OR the other is not present(the 'category data' can't be held in a single column," and so on for as many categories as are selected.

    Thanks for your help.

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Hide rows based on userform checkbox

    Try something like this. Rename your checkboxes to CheckBox1, CheckBox2... CheckBox10 so the code can loop through the checkboxes and reference them by number.

    Private Sub cmdSort_Click()
        
        Dim i As Long, rng As Range, arrCriteria As Variant
        
        Set rng = Rows(3)   'Headers
        arrCriteria = Array("Fire Extinguishers", "Chem", "FL", "Elec", "FP", _
                            "Lift", "PPE", "PS", "STF", "Ergonomics")
        
        Application.ScreenUpdating = False
        Rows.Hidden = False
        With Range("BC3:BL" & Range("A" & Rows.Count).End(xlUp).Row)
            For i = 1 To 10
                If Me.Controls("CheckBox" & i) Then
                    .AutoFilter i, arrCriteria(i - 1)
                    Set rng = Union(rng, .SpecialCells(xlCellTypeVisible).EntireRow)
                    .AutoFilter
                End If
            Next i
            .Parent.AutoFilterMode = False
            .EntireRow.Hidden = True
            rng.EntireRow.Hidden = False
        End With
        Application.ScreenUpdating = True
        
        Unload frmSort
        
    End Sub
    Also, technically this is a filter and not a sort

  5. #5
    Registered User
    Join Date
    12-11-2012
    Location
    cincinnati, ohio
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Hide rows based on userform checkbox

    @AlphaFrog, Thank you so much for that, it works PERFECTLY! Exactly what I was looking for! I'll have to take some time to review your code, it is far beyond what I could write myself! Thanks again!

  6. #6
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Hide rows based on userform checkbox

    I know that this a an old thread but I am trying to do something similar to this and tried to adapt this code to my needs but it only seems to filter by the first argument in the array..... I was wondering if anyone would be able to point me in the right direction to get this macro for my check boxes to function properly

    Private Sub btn_Submit_Click()
    Dim i As Long, rng As Range, arrCriteria As Variant

    Set rng = Rows(3) 'Headers
    arrCriteria = Array("Days 1", "Days 2", "Mids 1", "Mids 2", "Rotating", "SST", "SK305")

    Application.ScreenUpdating = False
    Rows.Hidden = False
    With Range("D6:NG" & Range("D" & Rows.Count).End(xlUp).Row)
    For i = 1 To 7
    If Me.Controls("CheckBox" & i) Then
    .AutoFilter i, arrCriteria(i - 1)
    Set rng = Union(rng, .SpecialCells(xlCellTypeVisible).EntireRow)
    .AutoFilter
    End If
    Next i
    .Parent.AutoFilterMode = False
    .EntireRow.Hidden = True
    rng.EntireRow.Hidden = False
    End With
    Application.ScreenUpdating = True

    Unload ch_Shift
    End Sub

  7. #7
    Valued Forum Contributor meabrams's Avatar
    Join Date
    10-03-2014
    Location
    USA
    MS-Off Ver
    2007, 2010, 2013, 2016
    Posts
    451

    Re: Hide rows based on userform checkbox

    Browser Glitched Delete this post please
    Last edited by meabrams; 01-21-2015 at 05:38 AM. Reason: repost brower glitch please delete this post

  8. #8
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Hide rows based on userform checkbox

    Welcome to the forum.

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

Closed 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