Need VBA macro to hide rows based on text cell values in 2 different columns












0














Below is the code I use to hide all rows with the value "Kitchen: in column 8. I also need to hide the row if column 12 contains "No". This is an "Or" statement, not an "And" statement.
I have researched the forum, but have not been able to find the answer. I would also like to speed up the process. Any help would be appreciated.



Sub FOHc()

BeginRow = 6
EndRow = 400
ChkCol = 8


For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True

Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False

End If
Next RowCnt

End Sub


Spreadsheet










share|improve this question





























    0














    Below is the code I use to hide all rows with the value "Kitchen: in column 8. I also need to hide the row if column 12 contains "No". This is an "Or" statement, not an "And" statement.
    I have researched the forum, but have not been able to find the answer. I would also like to speed up the process. Any help would be appreciated.



    Sub FOHc()

    BeginRow = 6
    EndRow = 400
    ChkCol = 8


    For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Then
    Cells(RowCnt, ChkCol).EntireRow.Hidden = True

    Else
    Cells(RowCnt, ChkCol).EntireRow.Hidden = False

    End If
    Next RowCnt

    End Sub


    Spreadsheet










    share|improve this question



























      0












      0








      0







      Below is the code I use to hide all rows with the value "Kitchen: in column 8. I also need to hide the row if column 12 contains "No". This is an "Or" statement, not an "And" statement.
      I have researched the forum, but have not been able to find the answer. I would also like to speed up the process. Any help would be appreciated.



      Sub FOHc()

      BeginRow = 6
      EndRow = 400
      ChkCol = 8


      For RowCnt = BeginRow To EndRow
      If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Then
      Cells(RowCnt, ChkCol).EntireRow.Hidden = True

      Else
      Cells(RowCnt, ChkCol).EntireRow.Hidden = False

      End If
      Next RowCnt

      End Sub


      Spreadsheet










      share|improve this question















      Below is the code I use to hide all rows with the value "Kitchen: in column 8. I also need to hide the row if column 12 contains "No". This is an "Or" statement, not an "And" statement.
      I have researched the forum, but have not been able to find the answer. I would also like to speed up the process. Any help would be appreciated.



      Sub FOHc()

      BeginRow = 6
      EndRow = 400
      ChkCol = 8


      For RowCnt = BeginRow To EndRow
      If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Then
      Cells(RowCnt, ChkCol).EntireRow.Hidden = True

      Else
      Cells(RowCnt, ChkCol).EntireRow.Hidden = False

      End If
      Next RowCnt

      End Sub


      Spreadsheet







      excel-vba hide rows vba excel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 9 at 19:34









      Community

      11




      11










      asked Jan 28 '16 at 18:28









      Kyle M

      1112




      1112
























          2 Answers
          2






          active

          oldest

          votes


















          0














          You can just add one more Or condition



          If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Or Cells(RowCnt, ChkCol2).Value = "No" Then


          For speeding up macro, add Application.ScreenUpdating = False at the beginning and Application.ScreenUpdating = True at the end. This will stop updating your screen while proceeding macro.






          share|improve this answer





























            0














            To add another logic test, simply add another Or statement.



            To speed up collect all rows to hide in a range, and hide them in one go.



            Also in general it's a good idea to use proper qualifiers, the statement Cells refers to what Excel considers to be the active sheet. while ThisWorkbook.Worksheets("Sheet1").Cells always refers to the sheet called "Sheet1".



            Declaring all variables may prevent unwanted behavior and bugs and in general is good practice.



            Here is the refactored code.



            Sub FOHc()
            Dim beginRow As Long
            Dim endRow As Long
            Dim chkCol As Long
            Dim rowCnt As Long
            Dim rngResult As Range
            Dim ws As Worksheet

            beginRow = 6
            endRow = 400
            chkCol = 8

            Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change this to the actual name of your sheet.

            With ws
            .Cells.EntireRow.Hidden = False 'Unhides all rows, remove line if that's not desired
            For rowCnt = beginRow To endRow
            If .Cells(rowCnt, chkCol) = "Kitchen" Or .Cells(rowCnt, chkCol) = Empty Or .Cells(rowCnt, 12) = "No" Then
            If rngResult Is Nothing Then
            Set rngResult = .Cells(rowCnt, 1)
            Else
            Set rngResult = Union(rngResult, .Cells(rowCnt, 1))
            End If
            End If
            Next rowCnt
            End With

            If Not rngResult Is Nothing Then rngResult.EntireRow.Hidden = True

            End Sub





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35069225%2fneed-vba-macro-to-hide-rows-based-on-text-cell-values-in-2-different-columns%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You can just add one more Or condition



              If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Or Cells(RowCnt, ChkCol2).Value = "No" Then


              For speeding up macro, add Application.ScreenUpdating = False at the beginning and Application.ScreenUpdating = True at the end. This will stop updating your screen while proceeding macro.






              share|improve this answer


























                0














                You can just add one more Or condition



                If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Or Cells(RowCnt, ChkCol2).Value = "No" Then


                For speeding up macro, add Application.ScreenUpdating = False at the beginning and Application.ScreenUpdating = True at the end. This will stop updating your screen while proceeding macro.






                share|improve this answer
























                  0












                  0








                  0






                  You can just add one more Or condition



                  If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Or Cells(RowCnt, ChkCol2).Value = "No" Then


                  For speeding up macro, add Application.ScreenUpdating = False at the beginning and Application.ScreenUpdating = True at the end. This will stop updating your screen while proceeding macro.






                  share|improve this answer












                  You can just add one more Or condition



                  If Cells(RowCnt, ChkCol).Value = "Kitchen" Or Cells(RowCnt, ChkCol).Value = Blank Or Cells(RowCnt, ChkCol2).Value = "No" Then


                  For speeding up macro, add Application.ScreenUpdating = False at the beginning and Application.ScreenUpdating = True at the end. This will stop updating your screen while proceeding macro.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 28 '16 at 18:45









                  Egan Wolf

                  2,7331722




                  2,7331722

























                      0














                      To add another logic test, simply add another Or statement.



                      To speed up collect all rows to hide in a range, and hide them in one go.



                      Also in general it's a good idea to use proper qualifiers, the statement Cells refers to what Excel considers to be the active sheet. while ThisWorkbook.Worksheets("Sheet1").Cells always refers to the sheet called "Sheet1".



                      Declaring all variables may prevent unwanted behavior and bugs and in general is good practice.



                      Here is the refactored code.



                      Sub FOHc()
                      Dim beginRow As Long
                      Dim endRow As Long
                      Dim chkCol As Long
                      Dim rowCnt As Long
                      Dim rngResult As Range
                      Dim ws As Worksheet

                      beginRow = 6
                      endRow = 400
                      chkCol = 8

                      Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change this to the actual name of your sheet.

                      With ws
                      .Cells.EntireRow.Hidden = False 'Unhides all rows, remove line if that's not desired
                      For rowCnt = beginRow To endRow
                      If .Cells(rowCnt, chkCol) = "Kitchen" Or .Cells(rowCnt, chkCol) = Empty Or .Cells(rowCnt, 12) = "No" Then
                      If rngResult Is Nothing Then
                      Set rngResult = .Cells(rowCnt, 1)
                      Else
                      Set rngResult = Union(rngResult, .Cells(rowCnt, 1))
                      End If
                      End If
                      Next rowCnt
                      End With

                      If Not rngResult Is Nothing Then rngResult.EntireRow.Hidden = True

                      End Sub





                      share|improve this answer




























                        0














                        To add another logic test, simply add another Or statement.



                        To speed up collect all rows to hide in a range, and hide them in one go.



                        Also in general it's a good idea to use proper qualifiers, the statement Cells refers to what Excel considers to be the active sheet. while ThisWorkbook.Worksheets("Sheet1").Cells always refers to the sheet called "Sheet1".



                        Declaring all variables may prevent unwanted behavior and bugs and in general is good practice.



                        Here is the refactored code.



                        Sub FOHc()
                        Dim beginRow As Long
                        Dim endRow As Long
                        Dim chkCol As Long
                        Dim rowCnt As Long
                        Dim rngResult As Range
                        Dim ws As Worksheet

                        beginRow = 6
                        endRow = 400
                        chkCol = 8

                        Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change this to the actual name of your sheet.

                        With ws
                        .Cells.EntireRow.Hidden = False 'Unhides all rows, remove line if that's not desired
                        For rowCnt = beginRow To endRow
                        If .Cells(rowCnt, chkCol) = "Kitchen" Or .Cells(rowCnt, chkCol) = Empty Or .Cells(rowCnt, 12) = "No" Then
                        If rngResult Is Nothing Then
                        Set rngResult = .Cells(rowCnt, 1)
                        Else
                        Set rngResult = Union(rngResult, .Cells(rowCnt, 1))
                        End If
                        End If
                        Next rowCnt
                        End With

                        If Not rngResult Is Nothing Then rngResult.EntireRow.Hidden = True

                        End Sub





                        share|improve this answer


























                          0












                          0








                          0






                          To add another logic test, simply add another Or statement.



                          To speed up collect all rows to hide in a range, and hide them in one go.



                          Also in general it's a good idea to use proper qualifiers, the statement Cells refers to what Excel considers to be the active sheet. while ThisWorkbook.Worksheets("Sheet1").Cells always refers to the sheet called "Sheet1".



                          Declaring all variables may prevent unwanted behavior and bugs and in general is good practice.



                          Here is the refactored code.



                          Sub FOHc()
                          Dim beginRow As Long
                          Dim endRow As Long
                          Dim chkCol As Long
                          Dim rowCnt As Long
                          Dim rngResult As Range
                          Dim ws As Worksheet

                          beginRow = 6
                          endRow = 400
                          chkCol = 8

                          Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change this to the actual name of your sheet.

                          With ws
                          .Cells.EntireRow.Hidden = False 'Unhides all rows, remove line if that's not desired
                          For rowCnt = beginRow To endRow
                          If .Cells(rowCnt, chkCol) = "Kitchen" Or .Cells(rowCnt, chkCol) = Empty Or .Cells(rowCnt, 12) = "No" Then
                          If rngResult Is Nothing Then
                          Set rngResult = .Cells(rowCnt, 1)
                          Else
                          Set rngResult = Union(rngResult, .Cells(rowCnt, 1))
                          End If
                          End If
                          Next rowCnt
                          End With

                          If Not rngResult Is Nothing Then rngResult.EntireRow.Hidden = True

                          End Sub





                          share|improve this answer














                          To add another logic test, simply add another Or statement.



                          To speed up collect all rows to hide in a range, and hide them in one go.



                          Also in general it's a good idea to use proper qualifiers, the statement Cells refers to what Excel considers to be the active sheet. while ThisWorkbook.Worksheets("Sheet1").Cells always refers to the sheet called "Sheet1".



                          Declaring all variables may prevent unwanted behavior and bugs and in general is good practice.



                          Here is the refactored code.



                          Sub FOHc()
                          Dim beginRow As Long
                          Dim endRow As Long
                          Dim chkCol As Long
                          Dim rowCnt As Long
                          Dim rngResult As Range
                          Dim ws As Worksheet

                          beginRow = 6
                          endRow = 400
                          chkCol = 8

                          Set ws = ThisWorkbook.Worksheets("Sheet1") 'Change this to the actual name of your sheet.

                          With ws
                          .Cells.EntireRow.Hidden = False 'Unhides all rows, remove line if that's not desired
                          For rowCnt = beginRow To endRow
                          If .Cells(rowCnt, chkCol) = "Kitchen" Or .Cells(rowCnt, chkCol) = Empty Or .Cells(rowCnt, 12) = "No" Then
                          If rngResult Is Nothing Then
                          Set rngResult = .Cells(rowCnt, 1)
                          Else
                          Set rngResult = Union(rngResult, .Cells(rowCnt, 1))
                          End If
                          End If
                          Next rowCnt
                          End With

                          If Not rngResult Is Nothing Then rngResult.EntireRow.Hidden = True

                          End Sub






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 28 '16 at 19:08

























                          answered Jan 28 '16 at 18:48









                          SilentRevolution

                          1,217623




                          1,217623






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35069225%2fneed-vba-macro-to-hide-rows-based-on-text-cell-values-in-2-different-columns%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings