Announcement

Collapse
No announcement yet.

Filtering with VBA?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Filtering with VBA?

    Bottom-line:
    I'm having difficulty getting a macro to filter as I need it to. In the below code, given both check boxes are marked, I want the spreadsheet to show both 0411 and 0431 rows (the filter criteria). Instead, the macro only shows 0431 since that subroutine comes after the 0411 one. (There will be about 20 such designators so I cannot use a combined check box)

    Code:
    Sub Run()
    If CheckBox21.Value = True Then
    Call Sort0411
    End If
    If CheckBox22.Value = True Then
    Call Sort0431
    End If
    End Sub
    Sub Sort0411()
    If CheckBox21.Value = True Then
        Columns("E:F").Hidden = False
        ActiveSheet.Range("$A$10:$BV$14").AutoFilter Field:=4, Criteria1:="=0411", _
        Operator:=xlOr, Criteria2:="="
        End If
    End Sub
    
    Sub Sort0431()
    If CheckBox22.Value = True Then
        Columns("G:H").Hidden = False
        ActiveSheet.Range("$A$10:$BV$14").AutoFilter Field:=4, Criteria1:="=0431", _
        Operator:=xlOr, Criteria2:="="
        End If
    End Sub
    More Information:

    Situation:
    I want to have a spreadsheet which tracks the training of individual Marines. Each of about 120 Marines are spread across about twenty occupational specialties. Each of these specialties has about 30 mission essential tasks. The first few columns will have identifying information while the rest will have headers for each mission essential task designator. A Marine will be placed on each row with the date of his achievement of each relevant task.

    This will mean the spreadsheet will have upwards of 600 columns, the vast majority of which will be blank for most Marines since those tasks will be outside of their occupational specialty.

    To resolve this for reporting and entry purposes, I want clickable check boxes (marked with the name of each occupational specialty) to show hidden irrelevant columns and use the autofilter to show the occupational specialty designator (which will be in one of the columns with identifying information).

    I plan to set it up so that a user can select any and all of the 20 check boxes (they are not mutually exclusive) and when he hits a button, the macro will show the appropriate hidden columns and filter accordingly.

    Problem:
    The showing of hidden columns works well but the filter does not. If more than 1 checkbox is selected (ie- 0411 and 0431 in the above code), I want the macro to show both 0411 and 0431. Instead, only 0431 will be shown since its filter comes after the 0411 one.


    Please help.

    Thank you.
    "Flutie was better than Kelly, Elway, Esiason and Cunningham." - Ben Kenobi
    "I have nothing against Wilson, but he's nowhere near the same calibre of QB as Flutie. Flutie threw for 5k+ yards in the CFL." -Ben Kenobi

  • #2
    first of all this is a report - so at best.. AT best ist's a sev4 ticket. - will never get looked at.

    sev 1 is like ew cant move product.

    Next - I would trust the first 25 google sitses byt just searching instead of the LD screw fest you'll find here.

    next... your a coder... COOL. I lpove working remote. Love for nmy brother! good luck.

    Comment


    • #3
      yeah drinkin

      Comment


      • #4
        Sounds logic.

        You only have two separate procedures, one for Checkbox 21 and one for Checkbox 22.

        If you want a separate sort behavior for having both checkboxes acitaved you have to program a third procedure where you state what you want if both Checkboxes are clicked.
        And then hide it behind a nested if-Statement.
        Like
        Code:
        If Checkbox 21 AND Checkbox22 then
             Call Procedure_C
        else
             If Checkbox21 then
                  Call Procedure_A
             elseif Checkbox22 then
                  Call Procedure_B
             Endif
        Endif
        Tamsin (Lost Girl): "I am the Harbinger of Death. I arrive on winds of blessed air. Air that you no longer deserve."
        Tamsin (Lost Girl): "He has fallen in battle and I must take him to the Einherjar in Valhalla"

        Comment


        • #5
          Read my post. That won't work. I have 20 different check boxes any number of which can be selected in any combination.
          "Flutie was better than Kelly, Elway, Esiason and Cunningham." - Ben Kenobi
          "I have nothing against Wilson, but he's nowhere near the same calibre of QB as Flutie. Flutie threw for 5k+ yards in the CFL." -Ben Kenobi

          Comment


          • #6
            Originally posted by Proteus_MST View Post
            Sounds logic.

            You only have two separate procedures, one for Checkbox 21 and one for Checkbox 22.

            If you want a separate sort behavior for having both checkboxes acitaved you have to program a third procedure where you state what you want if both Checkboxes are clicked.
            And then hide it behind a nested if-Statement.
            Like
            Code:
            If Checkbox 21 AND Checkbox22 then
                 Call Procedure_C
            else
                 If Checkbox21 then
                      Call Procedure_A
                 elseif Checkbox22 then
                      Call Procedure_B
                 Endif
            Endif

            see I like this. a lot of sites it's a pissing match. just some helpful example of the right direction to hgo or where in the config to look.

            nice.

            no one knows it all!

            Comment


            • #7
              than I read that didn't work, but still... nice.

              team player - on my team you work about 10 hgous a week!

              Comment


              • #8
                you don't want to set the bar too high. come one. quailuty of life mattersa.

                Comment


                • #9
                  Together
                  Everyone
                  Achieves
                  More
                  "Flutie was better than Kelly, Elway, Esiason and Cunningham." - Ben Kenobi
                  "I have nothing against Wilson, but he's nowhere near the same calibre of QB as Flutie. Flutie threw for 5k+ yards in the CFL." -Ben Kenobi

                  Comment


                  • #10
                    Originally posted by Al B. Sure! View Post
                    Read my post. That won't work. I have 20 different check boxes any number of which can be selected in any combination.
                    Then perhaps first check whether any of those checkboxes were checked (for example with "if Checkbox21 OR Checkbox22 ... OR Checknbox_n then")
                    and then call procedure_a

                    And in this procedure you
                    1. With 20 If-Clauses for each checkbox (or less if they overlap) determine what columns are hidden
                    2. In those 20 If-Clauses also assemble the Criteria-Statement via a String Variable.
                    3. Afterwards cut the last 2 chars to remove the comma used to separate the singular statements
                    4. Use the String as Criterion

                    For example
                    Code:
                    String strClause
                    strClause = "="
                    if checkbox21 then
                        Columns(E:F).Hidden = False
                        strClause = strClause & "0411, "
                    
                    endif
                    if Checkbox22 then
                        Columns(G:H).Hidden = False
                        strClause = strClause & "0431, "
                    endif
                    
                    ...
                    
                    'and in the end:
                    strClause = left(strClause, len(strClause)-2) 'To remove the last 2 cheracters which are ", "
                    
                    ActiveSheet.Range("$A$10:$BV$14").AutoFilter Field:=4, Criteria1:=strClause, _    
                     Operator:=xlOr, Criteria2:="="
                    Last edited by Proteus_MST; September 18, 2015, 19:24. Reason: Added something
                    Tamsin (Lost Girl): "I am the Harbinger of Death. I arrive on winds of blessed air. Air that you no longer deserve."
                    Tamsin (Lost Girl): "He has fallen in battle and I must take him to the Einherjar in Valhalla"

                    Comment


                    • #11
                      Does this look like Stackoverflow to you?
                      Graffiti in a public toilet
                      Do not require skill or wit
                      Among the **** we all are poets
                      Among the poets we are ****.

                      Comment


                      • #12
                        I am not an coder, so I have no idea if you have already tried these tips on extracting your specific data. I do know from experience to always shut that damn auto-filter off! Anyways, check out this link and see if it helps you to quickly resolve your dilemma. http://chandoo.org/wp/2012/11/27/ext...ubset-of-data/

                        Comment


                        • #13
                          Originally posted by Al B. Sure! View Post

                          Problem:
                          The showing of hidden columns works well but the filter does not. If more than 1 checkbox is selected (ie- 0411 and 0431 in the above code), I want the macro to show both 0411 and 0431. Instead, only 0431 will be shown since its filter comes after the 0411 one.
                          My guess is that you only can have one active at a time - try switch the calls of the two subs and see if 0411 is shown if last.

                          Oh, and get rid of those IF's in the subs - you know that they are TRUE.
                          With or without religion, you would have good people doing good things and evil people doing evil things. But for good people to do evil things, that takes religion.

                          Steven Weinberg

                          Comment

                          Working...
                          X