Hiding Columns in VBA excel












2















I have 4 worksheets in 1 excel workbook. I am attempting to store Sheet1, Sheet2, and Sheet4 in an array. Then, I am wanting the program to hide all columns in the array that do not have the value "FY" in row 8. The purpose of this is to be able to view the FY rows with ease and to leave Sheet3 from hiding any columns.



Here is a screenshot of the excel file:
enter image description here



and here is my code snippet:



Sub FY_HIDE222()    
Dim keyCells As Range
Dim ws As Variant
ws = Array("Sheet1", "Sheet2", "Sheet4")

For Each sh In ws
For Each keyCells In ws.Range("C8:ZZ8").Cells
If keyCells.Value <> "FY" Then
keyCells.EntireColumn.Hidden = True
End If
Next keyCells
Next sh
End Sub


I am receiving a syntax error on this line:



For Each keyCells In ws.Range("C8:ZZ8").Cells


saying "keyCells = nothing"...Please assist in my thinking as I am thinking the keyCells variable should have FY, Q1, Q2, etc. stored.










share|improve this question





























    2















    I have 4 worksheets in 1 excel workbook. I am attempting to store Sheet1, Sheet2, and Sheet4 in an array. Then, I am wanting the program to hide all columns in the array that do not have the value "FY" in row 8. The purpose of this is to be able to view the FY rows with ease and to leave Sheet3 from hiding any columns.



    Here is a screenshot of the excel file:
    enter image description here



    and here is my code snippet:



    Sub FY_HIDE222()    
    Dim keyCells As Range
    Dim ws As Variant
    ws = Array("Sheet1", "Sheet2", "Sheet4")

    For Each sh In ws
    For Each keyCells In ws.Range("C8:ZZ8").Cells
    If keyCells.Value <> "FY" Then
    keyCells.EntireColumn.Hidden = True
    End If
    Next keyCells
    Next sh
    End Sub


    I am receiving a syntax error on this line:



    For Each keyCells In ws.Range("C8:ZZ8").Cells


    saying "keyCells = nothing"...Please assist in my thinking as I am thinking the keyCells variable should have FY, Q1, Q2, etc. stored.










    share|improve this question



























      2












      2








      2


      1






      I have 4 worksheets in 1 excel workbook. I am attempting to store Sheet1, Sheet2, and Sheet4 in an array. Then, I am wanting the program to hide all columns in the array that do not have the value "FY" in row 8. The purpose of this is to be able to view the FY rows with ease and to leave Sheet3 from hiding any columns.



      Here is a screenshot of the excel file:
      enter image description here



      and here is my code snippet:



      Sub FY_HIDE222()    
      Dim keyCells As Range
      Dim ws As Variant
      ws = Array("Sheet1", "Sheet2", "Sheet4")

      For Each sh In ws
      For Each keyCells In ws.Range("C8:ZZ8").Cells
      If keyCells.Value <> "FY" Then
      keyCells.EntireColumn.Hidden = True
      End If
      Next keyCells
      Next sh
      End Sub


      I am receiving a syntax error on this line:



      For Each keyCells In ws.Range("C8:ZZ8").Cells


      saying "keyCells = nothing"...Please assist in my thinking as I am thinking the keyCells variable should have FY, Q1, Q2, etc. stored.










      share|improve this question
















      I have 4 worksheets in 1 excel workbook. I am attempting to store Sheet1, Sheet2, and Sheet4 in an array. Then, I am wanting the program to hide all columns in the array that do not have the value "FY" in row 8. The purpose of this is to be able to view the FY rows with ease and to leave Sheet3 from hiding any columns.



      Here is a screenshot of the excel file:
      enter image description here



      and here is my code snippet:



      Sub FY_HIDE222()    
      Dim keyCells As Range
      Dim ws As Variant
      ws = Array("Sheet1", "Sheet2", "Sheet4")

      For Each sh In ws
      For Each keyCells In ws.Range("C8:ZZ8").Cells
      If keyCells.Value <> "FY" Then
      keyCells.EntireColumn.Hidden = True
      End If
      Next keyCells
      Next sh
      End Sub


      I am receiving a syntax error on this line:



      For Each keyCells In ws.Range("C8:ZZ8").Cells


      saying "keyCells = nothing"...Please assist in my thinking as I am thinking the keyCells variable should have FY, Q1, Q2, etc. stored.







      excel vba excel-vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 8:00









      Pᴇʜ

      22.1k42750




      22.1k42750










      asked Nov 22 '18 at 23:45









      Tanner10Tanner10

      195




      195
























          4 Answers
          4






          active

          oldest

          votes


















          1














          Variable `sh is fetching string value only not Worksheet object. Try



          Sub FY_HIDE222()
          Dim keyCells As Range
          Dim wsName As Variant
          Dim ws As Worksheet
          wsName = Array("Sheet1", "Sheet2", "Sheet4")
          For Each sh In wsName
          Set ws = ThisWorkbook.Sheets(sh)
          For Each keyCells In ws.Range("C8:ZZ8").Cells
          If keyCells.Value <> "FY" Then
          keyCells.EntireColumn.Hidden = True
          End If
          Next keyCells
          Next sh
          End Sub





          share|improve this answer


























          • wsName is an array not an object, so you can not use for each. You have to use LBound and UBound.

            – VBasic2008
            Nov 23 '18 at 0:46








          • 1





            @VBasic2008 I agree in conventional coding that's the only way and may arise problem from option base.But why refrain from out of box thinking when the way it is used here by the author is working correct. objective is to pin point source of the error.

            – Ahmed AU
            Nov 23 '18 at 1:11








          • 1





            @QHarr: You are absolutely right, I've tested it (couldn't believe it). Sorry for spreading misinformation.

            – VBasic2008
            Nov 23 '18 at 11:18











          • Thank you both for the assistance. Ahmed, thank you for solving my initial error. VBasic, thank you for enlightening the Lbound/Ubound functionality to me. I went ahead and read a couple of articles on it, so much appreciated.

            – Tanner10
            Nov 23 '18 at 12:55











          • @Ahmed AU: My apologies for not recognizing that this was a working code. Since I always use Option Explicit the code failed on the first 'For Each' line and when I added 'Dim sh' it failed again on the second 'For Each' line, so I WRONGly assumed that the first 'For each' line was wrong (it had to be an 'LBound-UBound' approach, I thought). Since I use Excel 2003 there is no ZZ8, forgetting to change it to Z8 kept me from making it work.

            – VBasic2008
            Nov 23 '18 at 13:53



















          3














          To keep it as simple as possible



          Dim wsName, i As Long, c As Range
          wsName = Array("Sheet1", "Sheet2", "Sheet4")

          For i = LBound(wsName) To UBound(wsName)
          For Each c In Sheets(i).Range("C8:ZZ8")
          If c.Value <> "FY" Then c.EntireColumn.Hidden = True
          Next c
          Next i





          share|improve this answer


























          • Might be worth mentioning they could use Union so long as union object is set to nothing before next sheet.

            – QHarr
            Nov 23 '18 at 7:06











          • @QHarr - Since I only consider myself an intermediate at best. I have used Union to combine ranges in a worksheet, but that is my limit of usage. I would find it very educational to see an answer to this question using the Union method.

            – GMalc
            Nov 23 '18 at 11:10











          • Have done. In yours do you need Worksheets(wsName(i)) ? +1

            – QHarr
            Nov 23 '18 at 11:25













          • @QHarr - Yes sir

            – GMalc
            Nov 23 '18 at 11:28



















          2














          You can use union to gather qualifying ranges in the same sheet and hide in one go. If you want to go the extra mile you could re-write to loop the columns of the range and exit a loop of any given column at the first qualifying cell as you are hiding the entire column anyway.



          Public Sub test()
          Dim wsName, i As Long, c As Range, unionRng As Range
          wsName = Array("Sheet1", "Sheet2", "Sheet4")

          For i = LBound(wsName) To UBound(wsName)
          For Each c In Worksheets(wsName(i)).Range("C8:ZZ8")
          If c.Value <> "FY" Then
          If Not unionRng Is Nothing Then
          Set unionRng = Union(unionRng, c)
          Else
          Set unionRng = c
          End If
          End If
          Next c
          If Not unionRng Is Nothing Then
          unionRng.EntireColumn.Hidden = True
          Set unionRng = Nothing
          End If
          Next i
          End Sub





          share|improve this answer



















          • 1





            Recap, for each sheet you combine every column that has "FY" using Union and then hide all at the same time. Then you reset the unionRng back to nothing before the next sheet. Thanks, outstanding +1

            – GMalc
            Nov 23 '18 at 11:39








          • 1





            yes as union doesn't work across sheets

            – QHarr
            Nov 23 '18 at 11:47











          • Since the OP's image indicates that there are fewer cells containing "FY" you could hide the entire range (after 'For i = LBound(wsName) To UBound(wsName)') with 'Sheets(wsName(i)).Range("C8:Z8").EntireColumn.Hidden = True' and then change the lines 'If c.Value = "FY" Then' and 'If Not unionRng Is Nothing Then unionRng.EntireColumn.Hidden = False' to make it even more efficient (faster).

            – VBasic2008
            Nov 23 '18 at 12:31











          • I would still have to loop the same number of columns though?

            – QHarr
            Nov 23 '18 at 12:45











          • @QHarr: That's true, but I guess it takes less time to hide all columns and then to show the few containing "FY" than to hide the many columns not containing "FY". In my sample 4 out of 24 columns the code finished almost twice as fast In a sample 4 out of 254 it was almost 20 times faster. Of course this is all hairsplitting since we're talking about milliseconds. My comment wasn't about criticizing your code but to indicate that there is another approach. BTW what's the extra mile all about?

            – VBasic2008
            Nov 23 '18 at 13:21



















          1














          Show Some, Show All



          enter image description here



          Option Explicit

          Sub FY_HIDE222()

          Const cStrRange = "C8:ZZ8"
          Const cStrText = "FY"

          Dim objWs As Worksheet
          Dim objCell As Range
          Dim vntSheets As Variant
          Dim intCounter As Integer

          vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

          For intCounter = LBound(vntSheets) To UBound(vntSheets)

          Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

          For Each objCell In objWs.Range(cStrRange)

          If objCell.Value <> cStrText Then
          objCell.EntireColumn.Hidden = True
          End If

          Next

          Next

          End Sub


          I have Excel 2003, so I used Z8 instead of ZZ8 which is obvious in the following picture.



          enter image description here



          Sub FY_SHOW222()

          Dim objWs As Worksheet
          Dim vntSheets As Variant
          Dim intCounter As Integer

          vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

          For intCounter = LBound(vntSheets) To UBound(vntSheets)

          Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

          objWs.Columns.EntireColumn.Hidden = False

          Next

          End Sub


          enter image description here






          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%2f53439161%2fhiding-columns-in-vba-excel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Variable `sh is fetching string value only not Worksheet object. Try



            Sub FY_HIDE222()
            Dim keyCells As Range
            Dim wsName As Variant
            Dim ws As Worksheet
            wsName = Array("Sheet1", "Sheet2", "Sheet4")
            For Each sh In wsName
            Set ws = ThisWorkbook.Sheets(sh)
            For Each keyCells In ws.Range("C8:ZZ8").Cells
            If keyCells.Value <> "FY" Then
            keyCells.EntireColumn.Hidden = True
            End If
            Next keyCells
            Next sh
            End Sub





            share|improve this answer


























            • wsName is an array not an object, so you can not use for each. You have to use LBound and UBound.

              – VBasic2008
              Nov 23 '18 at 0:46








            • 1





              @VBasic2008 I agree in conventional coding that's the only way and may arise problem from option base.But why refrain from out of box thinking when the way it is used here by the author is working correct. objective is to pin point source of the error.

              – Ahmed AU
              Nov 23 '18 at 1:11








            • 1





              @QHarr: You are absolutely right, I've tested it (couldn't believe it). Sorry for spreading misinformation.

              – VBasic2008
              Nov 23 '18 at 11:18











            • Thank you both for the assistance. Ahmed, thank you for solving my initial error. VBasic, thank you for enlightening the Lbound/Ubound functionality to me. I went ahead and read a couple of articles on it, so much appreciated.

              – Tanner10
              Nov 23 '18 at 12:55











            • @Ahmed AU: My apologies for not recognizing that this was a working code. Since I always use Option Explicit the code failed on the first 'For Each' line and when I added 'Dim sh' it failed again on the second 'For Each' line, so I WRONGly assumed that the first 'For each' line was wrong (it had to be an 'LBound-UBound' approach, I thought). Since I use Excel 2003 there is no ZZ8, forgetting to change it to Z8 kept me from making it work.

              – VBasic2008
              Nov 23 '18 at 13:53
















            1














            Variable `sh is fetching string value only not Worksheet object. Try



            Sub FY_HIDE222()
            Dim keyCells As Range
            Dim wsName As Variant
            Dim ws As Worksheet
            wsName = Array("Sheet1", "Sheet2", "Sheet4")
            For Each sh In wsName
            Set ws = ThisWorkbook.Sheets(sh)
            For Each keyCells In ws.Range("C8:ZZ8").Cells
            If keyCells.Value <> "FY" Then
            keyCells.EntireColumn.Hidden = True
            End If
            Next keyCells
            Next sh
            End Sub





            share|improve this answer


























            • wsName is an array not an object, so you can not use for each. You have to use LBound and UBound.

              – VBasic2008
              Nov 23 '18 at 0:46








            • 1





              @VBasic2008 I agree in conventional coding that's the only way and may arise problem from option base.But why refrain from out of box thinking when the way it is used here by the author is working correct. objective is to pin point source of the error.

              – Ahmed AU
              Nov 23 '18 at 1:11








            • 1





              @QHarr: You are absolutely right, I've tested it (couldn't believe it). Sorry for spreading misinformation.

              – VBasic2008
              Nov 23 '18 at 11:18











            • Thank you both for the assistance. Ahmed, thank you for solving my initial error. VBasic, thank you for enlightening the Lbound/Ubound functionality to me. I went ahead and read a couple of articles on it, so much appreciated.

              – Tanner10
              Nov 23 '18 at 12:55











            • @Ahmed AU: My apologies for not recognizing that this was a working code. Since I always use Option Explicit the code failed on the first 'For Each' line and when I added 'Dim sh' it failed again on the second 'For Each' line, so I WRONGly assumed that the first 'For each' line was wrong (it had to be an 'LBound-UBound' approach, I thought). Since I use Excel 2003 there is no ZZ8, forgetting to change it to Z8 kept me from making it work.

              – VBasic2008
              Nov 23 '18 at 13:53














            1












            1








            1







            Variable `sh is fetching string value only not Worksheet object. Try



            Sub FY_HIDE222()
            Dim keyCells As Range
            Dim wsName As Variant
            Dim ws As Worksheet
            wsName = Array("Sheet1", "Sheet2", "Sheet4")
            For Each sh In wsName
            Set ws = ThisWorkbook.Sheets(sh)
            For Each keyCells In ws.Range("C8:ZZ8").Cells
            If keyCells.Value <> "FY" Then
            keyCells.EntireColumn.Hidden = True
            End If
            Next keyCells
            Next sh
            End Sub





            share|improve this answer















            Variable `sh is fetching string value only not Worksheet object. Try



            Sub FY_HIDE222()
            Dim keyCells As Range
            Dim wsName As Variant
            Dim ws As Worksheet
            wsName = Array("Sheet1", "Sheet2", "Sheet4")
            For Each sh In wsName
            Set ws = ThisWorkbook.Sheets(sh)
            For Each keyCells In ws.Range("C8:ZZ8").Cells
            If keyCells.Value <> "FY" Then
            keyCells.EntireColumn.Hidden = True
            End If
            Next keyCells
            Next sh
            End Sub






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 0:44

























            answered Nov 23 '18 at 0:37









            Ahmed AUAhmed AU

            88528




            88528













            • wsName is an array not an object, so you can not use for each. You have to use LBound and UBound.

              – VBasic2008
              Nov 23 '18 at 0:46








            • 1





              @VBasic2008 I agree in conventional coding that's the only way and may arise problem from option base.But why refrain from out of box thinking when the way it is used here by the author is working correct. objective is to pin point source of the error.

              – Ahmed AU
              Nov 23 '18 at 1:11








            • 1





              @QHarr: You are absolutely right, I've tested it (couldn't believe it). Sorry for spreading misinformation.

              – VBasic2008
              Nov 23 '18 at 11:18











            • Thank you both for the assistance. Ahmed, thank you for solving my initial error. VBasic, thank you for enlightening the Lbound/Ubound functionality to me. I went ahead and read a couple of articles on it, so much appreciated.

              – Tanner10
              Nov 23 '18 at 12:55











            • @Ahmed AU: My apologies for not recognizing that this was a working code. Since I always use Option Explicit the code failed on the first 'For Each' line and when I added 'Dim sh' it failed again on the second 'For Each' line, so I WRONGly assumed that the first 'For each' line was wrong (it had to be an 'LBound-UBound' approach, I thought). Since I use Excel 2003 there is no ZZ8, forgetting to change it to Z8 kept me from making it work.

              – VBasic2008
              Nov 23 '18 at 13:53



















            • wsName is an array not an object, so you can not use for each. You have to use LBound and UBound.

              – VBasic2008
              Nov 23 '18 at 0:46








            • 1





              @VBasic2008 I agree in conventional coding that's the only way and may arise problem from option base.But why refrain from out of box thinking when the way it is used here by the author is working correct. objective is to pin point source of the error.

              – Ahmed AU
              Nov 23 '18 at 1:11








            • 1





              @QHarr: You are absolutely right, I've tested it (couldn't believe it). Sorry for spreading misinformation.

              – VBasic2008
              Nov 23 '18 at 11:18











            • Thank you both for the assistance. Ahmed, thank you for solving my initial error. VBasic, thank you for enlightening the Lbound/Ubound functionality to me. I went ahead and read a couple of articles on it, so much appreciated.

              – Tanner10
              Nov 23 '18 at 12:55











            • @Ahmed AU: My apologies for not recognizing that this was a working code. Since I always use Option Explicit the code failed on the first 'For Each' line and when I added 'Dim sh' it failed again on the second 'For Each' line, so I WRONGly assumed that the first 'For each' line was wrong (it had to be an 'LBound-UBound' approach, I thought). Since I use Excel 2003 there is no ZZ8, forgetting to change it to Z8 kept me from making it work.

              – VBasic2008
              Nov 23 '18 at 13:53

















            wsName is an array not an object, so you can not use for each. You have to use LBound and UBound.

            – VBasic2008
            Nov 23 '18 at 0:46







            wsName is an array not an object, so you can not use for each. You have to use LBound and UBound.

            – VBasic2008
            Nov 23 '18 at 0:46






            1




            1





            @VBasic2008 I agree in conventional coding that's the only way and may arise problem from option base.But why refrain from out of box thinking when the way it is used here by the author is working correct. objective is to pin point source of the error.

            – Ahmed AU
            Nov 23 '18 at 1:11







            @VBasic2008 I agree in conventional coding that's the only way and may arise problem from option base.But why refrain from out of box thinking when the way it is used here by the author is working correct. objective is to pin point source of the error.

            – Ahmed AU
            Nov 23 '18 at 1:11






            1




            1





            @QHarr: You are absolutely right, I've tested it (couldn't believe it). Sorry for spreading misinformation.

            – VBasic2008
            Nov 23 '18 at 11:18





            @QHarr: You are absolutely right, I've tested it (couldn't believe it). Sorry for spreading misinformation.

            – VBasic2008
            Nov 23 '18 at 11:18













            Thank you both for the assistance. Ahmed, thank you for solving my initial error. VBasic, thank you for enlightening the Lbound/Ubound functionality to me. I went ahead and read a couple of articles on it, so much appreciated.

            – Tanner10
            Nov 23 '18 at 12:55





            Thank you both for the assistance. Ahmed, thank you for solving my initial error. VBasic, thank you for enlightening the Lbound/Ubound functionality to me. I went ahead and read a couple of articles on it, so much appreciated.

            – Tanner10
            Nov 23 '18 at 12:55













            @Ahmed AU: My apologies for not recognizing that this was a working code. Since I always use Option Explicit the code failed on the first 'For Each' line and when I added 'Dim sh' it failed again on the second 'For Each' line, so I WRONGly assumed that the first 'For each' line was wrong (it had to be an 'LBound-UBound' approach, I thought). Since I use Excel 2003 there is no ZZ8, forgetting to change it to Z8 kept me from making it work.

            – VBasic2008
            Nov 23 '18 at 13:53





            @Ahmed AU: My apologies for not recognizing that this was a working code. Since I always use Option Explicit the code failed on the first 'For Each' line and when I added 'Dim sh' it failed again on the second 'For Each' line, so I WRONGly assumed that the first 'For each' line was wrong (it had to be an 'LBound-UBound' approach, I thought). Since I use Excel 2003 there is no ZZ8, forgetting to change it to Z8 kept me from making it work.

            – VBasic2008
            Nov 23 '18 at 13:53













            3














            To keep it as simple as possible



            Dim wsName, i As Long, c As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Sheets(i).Range("C8:ZZ8")
            If c.Value <> "FY" Then c.EntireColumn.Hidden = True
            Next c
            Next i





            share|improve this answer


























            • Might be worth mentioning they could use Union so long as union object is set to nothing before next sheet.

              – QHarr
              Nov 23 '18 at 7:06











            • @QHarr - Since I only consider myself an intermediate at best. I have used Union to combine ranges in a worksheet, but that is my limit of usage. I would find it very educational to see an answer to this question using the Union method.

              – GMalc
              Nov 23 '18 at 11:10











            • Have done. In yours do you need Worksheets(wsName(i)) ? +1

              – QHarr
              Nov 23 '18 at 11:25













            • @QHarr - Yes sir

              – GMalc
              Nov 23 '18 at 11:28
















            3














            To keep it as simple as possible



            Dim wsName, i As Long, c As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Sheets(i).Range("C8:ZZ8")
            If c.Value <> "FY" Then c.EntireColumn.Hidden = True
            Next c
            Next i





            share|improve this answer


























            • Might be worth mentioning they could use Union so long as union object is set to nothing before next sheet.

              – QHarr
              Nov 23 '18 at 7:06











            • @QHarr - Since I only consider myself an intermediate at best. I have used Union to combine ranges in a worksheet, but that is my limit of usage. I would find it very educational to see an answer to this question using the Union method.

              – GMalc
              Nov 23 '18 at 11:10











            • Have done. In yours do you need Worksheets(wsName(i)) ? +1

              – QHarr
              Nov 23 '18 at 11:25













            • @QHarr - Yes sir

              – GMalc
              Nov 23 '18 at 11:28














            3












            3








            3







            To keep it as simple as possible



            Dim wsName, i As Long, c As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Sheets(i).Range("C8:ZZ8")
            If c.Value <> "FY" Then c.EntireColumn.Hidden = True
            Next c
            Next i





            share|improve this answer















            To keep it as simple as possible



            Dim wsName, i As Long, c As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Sheets(i).Range("C8:ZZ8")
            If c.Value <> "FY" Then c.EntireColumn.Hidden = True
            Next c
            Next i






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 9:57

























            answered Nov 23 '18 at 1:10









            GMalcGMalc

            1,0411410




            1,0411410













            • Might be worth mentioning they could use Union so long as union object is set to nothing before next sheet.

              – QHarr
              Nov 23 '18 at 7:06











            • @QHarr - Since I only consider myself an intermediate at best. I have used Union to combine ranges in a worksheet, but that is my limit of usage. I would find it very educational to see an answer to this question using the Union method.

              – GMalc
              Nov 23 '18 at 11:10











            • Have done. In yours do you need Worksheets(wsName(i)) ? +1

              – QHarr
              Nov 23 '18 at 11:25













            • @QHarr - Yes sir

              – GMalc
              Nov 23 '18 at 11:28



















            • Might be worth mentioning they could use Union so long as union object is set to nothing before next sheet.

              – QHarr
              Nov 23 '18 at 7:06











            • @QHarr - Since I only consider myself an intermediate at best. I have used Union to combine ranges in a worksheet, but that is my limit of usage. I would find it very educational to see an answer to this question using the Union method.

              – GMalc
              Nov 23 '18 at 11:10











            • Have done. In yours do you need Worksheets(wsName(i)) ? +1

              – QHarr
              Nov 23 '18 at 11:25













            • @QHarr - Yes sir

              – GMalc
              Nov 23 '18 at 11:28

















            Might be worth mentioning they could use Union so long as union object is set to nothing before next sheet.

            – QHarr
            Nov 23 '18 at 7:06





            Might be worth mentioning they could use Union so long as union object is set to nothing before next sheet.

            – QHarr
            Nov 23 '18 at 7:06













            @QHarr - Since I only consider myself an intermediate at best. I have used Union to combine ranges in a worksheet, but that is my limit of usage. I would find it very educational to see an answer to this question using the Union method.

            – GMalc
            Nov 23 '18 at 11:10





            @QHarr - Since I only consider myself an intermediate at best. I have used Union to combine ranges in a worksheet, but that is my limit of usage. I would find it very educational to see an answer to this question using the Union method.

            – GMalc
            Nov 23 '18 at 11:10













            Have done. In yours do you need Worksheets(wsName(i)) ? +1

            – QHarr
            Nov 23 '18 at 11:25







            Have done. In yours do you need Worksheets(wsName(i)) ? +1

            – QHarr
            Nov 23 '18 at 11:25















            @QHarr - Yes sir

            – GMalc
            Nov 23 '18 at 11:28





            @QHarr - Yes sir

            – GMalc
            Nov 23 '18 at 11:28











            2














            You can use union to gather qualifying ranges in the same sheet and hide in one go. If you want to go the extra mile you could re-write to loop the columns of the range and exit a loop of any given column at the first qualifying cell as you are hiding the entire column anyway.



            Public Sub test()
            Dim wsName, i As Long, c As Range, unionRng As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Worksheets(wsName(i)).Range("C8:ZZ8")
            If c.Value <> "FY" Then
            If Not unionRng Is Nothing Then
            Set unionRng = Union(unionRng, c)
            Else
            Set unionRng = c
            End If
            End If
            Next c
            If Not unionRng Is Nothing Then
            unionRng.EntireColumn.Hidden = True
            Set unionRng = Nothing
            End If
            Next i
            End Sub





            share|improve this answer



















            • 1





              Recap, for each sheet you combine every column that has "FY" using Union and then hide all at the same time. Then you reset the unionRng back to nothing before the next sheet. Thanks, outstanding +1

              – GMalc
              Nov 23 '18 at 11:39








            • 1





              yes as union doesn't work across sheets

              – QHarr
              Nov 23 '18 at 11:47











            • Since the OP's image indicates that there are fewer cells containing "FY" you could hide the entire range (after 'For i = LBound(wsName) To UBound(wsName)') with 'Sheets(wsName(i)).Range("C8:Z8").EntireColumn.Hidden = True' and then change the lines 'If c.Value = "FY" Then' and 'If Not unionRng Is Nothing Then unionRng.EntireColumn.Hidden = False' to make it even more efficient (faster).

              – VBasic2008
              Nov 23 '18 at 12:31











            • I would still have to loop the same number of columns though?

              – QHarr
              Nov 23 '18 at 12:45











            • @QHarr: That's true, but I guess it takes less time to hide all columns and then to show the few containing "FY" than to hide the many columns not containing "FY". In my sample 4 out of 24 columns the code finished almost twice as fast In a sample 4 out of 254 it was almost 20 times faster. Of course this is all hairsplitting since we're talking about milliseconds. My comment wasn't about criticizing your code but to indicate that there is another approach. BTW what's the extra mile all about?

              – VBasic2008
              Nov 23 '18 at 13:21
















            2














            You can use union to gather qualifying ranges in the same sheet and hide in one go. If you want to go the extra mile you could re-write to loop the columns of the range and exit a loop of any given column at the first qualifying cell as you are hiding the entire column anyway.



            Public Sub test()
            Dim wsName, i As Long, c As Range, unionRng As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Worksheets(wsName(i)).Range("C8:ZZ8")
            If c.Value <> "FY" Then
            If Not unionRng Is Nothing Then
            Set unionRng = Union(unionRng, c)
            Else
            Set unionRng = c
            End If
            End If
            Next c
            If Not unionRng Is Nothing Then
            unionRng.EntireColumn.Hidden = True
            Set unionRng = Nothing
            End If
            Next i
            End Sub





            share|improve this answer



















            • 1





              Recap, for each sheet you combine every column that has "FY" using Union and then hide all at the same time. Then you reset the unionRng back to nothing before the next sheet. Thanks, outstanding +1

              – GMalc
              Nov 23 '18 at 11:39








            • 1





              yes as union doesn't work across sheets

              – QHarr
              Nov 23 '18 at 11:47











            • Since the OP's image indicates that there are fewer cells containing "FY" you could hide the entire range (after 'For i = LBound(wsName) To UBound(wsName)') with 'Sheets(wsName(i)).Range("C8:Z8").EntireColumn.Hidden = True' and then change the lines 'If c.Value = "FY" Then' and 'If Not unionRng Is Nothing Then unionRng.EntireColumn.Hidden = False' to make it even more efficient (faster).

              – VBasic2008
              Nov 23 '18 at 12:31











            • I would still have to loop the same number of columns though?

              – QHarr
              Nov 23 '18 at 12:45











            • @QHarr: That's true, but I guess it takes less time to hide all columns and then to show the few containing "FY" than to hide the many columns not containing "FY". In my sample 4 out of 24 columns the code finished almost twice as fast In a sample 4 out of 254 it was almost 20 times faster. Of course this is all hairsplitting since we're talking about milliseconds. My comment wasn't about criticizing your code but to indicate that there is another approach. BTW what's the extra mile all about?

              – VBasic2008
              Nov 23 '18 at 13:21














            2












            2








            2







            You can use union to gather qualifying ranges in the same sheet and hide in one go. If you want to go the extra mile you could re-write to loop the columns of the range and exit a loop of any given column at the first qualifying cell as you are hiding the entire column anyway.



            Public Sub test()
            Dim wsName, i As Long, c As Range, unionRng As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Worksheets(wsName(i)).Range("C8:ZZ8")
            If c.Value <> "FY" Then
            If Not unionRng Is Nothing Then
            Set unionRng = Union(unionRng, c)
            Else
            Set unionRng = c
            End If
            End If
            Next c
            If Not unionRng Is Nothing Then
            unionRng.EntireColumn.Hidden = True
            Set unionRng = Nothing
            End If
            Next i
            End Sub





            share|improve this answer













            You can use union to gather qualifying ranges in the same sheet and hide in one go. If you want to go the extra mile you could re-write to loop the columns of the range and exit a loop of any given column at the first qualifying cell as you are hiding the entire column anyway.



            Public Sub test()
            Dim wsName, i As Long, c As Range, unionRng As Range
            wsName = Array("Sheet1", "Sheet2", "Sheet4")

            For i = LBound(wsName) To UBound(wsName)
            For Each c In Worksheets(wsName(i)).Range("C8:ZZ8")
            If c.Value <> "FY" Then
            If Not unionRng Is Nothing Then
            Set unionRng = Union(unionRng, c)
            Else
            Set unionRng = c
            End If
            End If
            Next c
            If Not unionRng Is Nothing Then
            unionRng.EntireColumn.Hidden = True
            Set unionRng = Nothing
            End If
            Next i
            End Sub






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 11:24









            QHarrQHarr

            32k82042




            32k82042








            • 1





              Recap, for each sheet you combine every column that has "FY" using Union and then hide all at the same time. Then you reset the unionRng back to nothing before the next sheet. Thanks, outstanding +1

              – GMalc
              Nov 23 '18 at 11:39








            • 1





              yes as union doesn't work across sheets

              – QHarr
              Nov 23 '18 at 11:47











            • Since the OP's image indicates that there are fewer cells containing "FY" you could hide the entire range (after 'For i = LBound(wsName) To UBound(wsName)') with 'Sheets(wsName(i)).Range("C8:Z8").EntireColumn.Hidden = True' and then change the lines 'If c.Value = "FY" Then' and 'If Not unionRng Is Nothing Then unionRng.EntireColumn.Hidden = False' to make it even more efficient (faster).

              – VBasic2008
              Nov 23 '18 at 12:31











            • I would still have to loop the same number of columns though?

              – QHarr
              Nov 23 '18 at 12:45











            • @QHarr: That's true, but I guess it takes less time to hide all columns and then to show the few containing "FY" than to hide the many columns not containing "FY". In my sample 4 out of 24 columns the code finished almost twice as fast In a sample 4 out of 254 it was almost 20 times faster. Of course this is all hairsplitting since we're talking about milliseconds. My comment wasn't about criticizing your code but to indicate that there is another approach. BTW what's the extra mile all about?

              – VBasic2008
              Nov 23 '18 at 13:21














            • 1





              Recap, for each sheet you combine every column that has "FY" using Union and then hide all at the same time. Then you reset the unionRng back to nothing before the next sheet. Thanks, outstanding +1

              – GMalc
              Nov 23 '18 at 11:39








            • 1





              yes as union doesn't work across sheets

              – QHarr
              Nov 23 '18 at 11:47











            • Since the OP's image indicates that there are fewer cells containing "FY" you could hide the entire range (after 'For i = LBound(wsName) To UBound(wsName)') with 'Sheets(wsName(i)).Range("C8:Z8").EntireColumn.Hidden = True' and then change the lines 'If c.Value = "FY" Then' and 'If Not unionRng Is Nothing Then unionRng.EntireColumn.Hidden = False' to make it even more efficient (faster).

              – VBasic2008
              Nov 23 '18 at 12:31











            • I would still have to loop the same number of columns though?

              – QHarr
              Nov 23 '18 at 12:45











            • @QHarr: That's true, but I guess it takes less time to hide all columns and then to show the few containing "FY" than to hide the many columns not containing "FY". In my sample 4 out of 24 columns the code finished almost twice as fast In a sample 4 out of 254 it was almost 20 times faster. Of course this is all hairsplitting since we're talking about milliseconds. My comment wasn't about criticizing your code but to indicate that there is another approach. BTW what's the extra mile all about?

              – VBasic2008
              Nov 23 '18 at 13:21








            1




            1





            Recap, for each sheet you combine every column that has "FY" using Union and then hide all at the same time. Then you reset the unionRng back to nothing before the next sheet. Thanks, outstanding +1

            – GMalc
            Nov 23 '18 at 11:39







            Recap, for each sheet you combine every column that has "FY" using Union and then hide all at the same time. Then you reset the unionRng back to nothing before the next sheet. Thanks, outstanding +1

            – GMalc
            Nov 23 '18 at 11:39






            1




            1





            yes as union doesn't work across sheets

            – QHarr
            Nov 23 '18 at 11:47





            yes as union doesn't work across sheets

            – QHarr
            Nov 23 '18 at 11:47













            Since the OP's image indicates that there are fewer cells containing "FY" you could hide the entire range (after 'For i = LBound(wsName) To UBound(wsName)') with 'Sheets(wsName(i)).Range("C8:Z8").EntireColumn.Hidden = True' and then change the lines 'If c.Value = "FY" Then' and 'If Not unionRng Is Nothing Then unionRng.EntireColumn.Hidden = False' to make it even more efficient (faster).

            – VBasic2008
            Nov 23 '18 at 12:31





            Since the OP's image indicates that there are fewer cells containing "FY" you could hide the entire range (after 'For i = LBound(wsName) To UBound(wsName)') with 'Sheets(wsName(i)).Range("C8:Z8").EntireColumn.Hidden = True' and then change the lines 'If c.Value = "FY" Then' and 'If Not unionRng Is Nothing Then unionRng.EntireColumn.Hidden = False' to make it even more efficient (faster).

            – VBasic2008
            Nov 23 '18 at 12:31













            I would still have to loop the same number of columns though?

            – QHarr
            Nov 23 '18 at 12:45





            I would still have to loop the same number of columns though?

            – QHarr
            Nov 23 '18 at 12:45













            @QHarr: That's true, but I guess it takes less time to hide all columns and then to show the few containing "FY" than to hide the many columns not containing "FY". In my sample 4 out of 24 columns the code finished almost twice as fast In a sample 4 out of 254 it was almost 20 times faster. Of course this is all hairsplitting since we're talking about milliseconds. My comment wasn't about criticizing your code but to indicate that there is another approach. BTW what's the extra mile all about?

            – VBasic2008
            Nov 23 '18 at 13:21





            @QHarr: That's true, but I guess it takes less time to hide all columns and then to show the few containing "FY" than to hide the many columns not containing "FY". In my sample 4 out of 24 columns the code finished almost twice as fast In a sample 4 out of 254 it was almost 20 times faster. Of course this is all hairsplitting since we're talking about milliseconds. My comment wasn't about criticizing your code but to indicate that there is another approach. BTW what's the extra mile all about?

            – VBasic2008
            Nov 23 '18 at 13:21











            1














            Show Some, Show All



            enter image description here



            Option Explicit

            Sub FY_HIDE222()

            Const cStrRange = "C8:ZZ8"
            Const cStrText = "FY"

            Dim objWs As Worksheet
            Dim objCell As Range
            Dim vntSheets As Variant
            Dim intCounter As Integer

            vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

            For intCounter = LBound(vntSheets) To UBound(vntSheets)

            Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

            For Each objCell In objWs.Range(cStrRange)

            If objCell.Value <> cStrText Then
            objCell.EntireColumn.Hidden = True
            End If

            Next

            Next

            End Sub


            I have Excel 2003, so I used Z8 instead of ZZ8 which is obvious in the following picture.



            enter image description here



            Sub FY_SHOW222()

            Dim objWs As Worksheet
            Dim vntSheets As Variant
            Dim intCounter As Integer

            vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

            For intCounter = LBound(vntSheets) To UBound(vntSheets)

            Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

            objWs.Columns.EntireColumn.Hidden = False

            Next

            End Sub


            enter image description here






            share|improve this answer




























              1














              Show Some, Show All



              enter image description here



              Option Explicit

              Sub FY_HIDE222()

              Const cStrRange = "C8:ZZ8"
              Const cStrText = "FY"

              Dim objWs As Worksheet
              Dim objCell As Range
              Dim vntSheets As Variant
              Dim intCounter As Integer

              vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

              For intCounter = LBound(vntSheets) To UBound(vntSheets)

              Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

              For Each objCell In objWs.Range(cStrRange)

              If objCell.Value <> cStrText Then
              objCell.EntireColumn.Hidden = True
              End If

              Next

              Next

              End Sub


              I have Excel 2003, so I used Z8 instead of ZZ8 which is obvious in the following picture.



              enter image description here



              Sub FY_SHOW222()

              Dim objWs As Worksheet
              Dim vntSheets As Variant
              Dim intCounter As Integer

              vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

              For intCounter = LBound(vntSheets) To UBound(vntSheets)

              Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

              objWs.Columns.EntireColumn.Hidden = False

              Next

              End Sub


              enter image description here






              share|improve this answer


























                1












                1








                1







                Show Some, Show All



                enter image description here



                Option Explicit

                Sub FY_HIDE222()

                Const cStrRange = "C8:ZZ8"
                Const cStrText = "FY"

                Dim objWs As Worksheet
                Dim objCell As Range
                Dim vntSheets As Variant
                Dim intCounter As Integer

                vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

                For intCounter = LBound(vntSheets) To UBound(vntSheets)

                Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

                For Each objCell In objWs.Range(cStrRange)

                If objCell.Value <> cStrText Then
                objCell.EntireColumn.Hidden = True
                End If

                Next

                Next

                End Sub


                I have Excel 2003, so I used Z8 instead of ZZ8 which is obvious in the following picture.



                enter image description here



                Sub FY_SHOW222()

                Dim objWs As Worksheet
                Dim vntSheets As Variant
                Dim intCounter As Integer

                vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

                For intCounter = LBound(vntSheets) To UBound(vntSheets)

                Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

                objWs.Columns.EntireColumn.Hidden = False

                Next

                End Sub


                enter image description here






                share|improve this answer













                Show Some, Show All



                enter image description here



                Option Explicit

                Sub FY_HIDE222()

                Const cStrRange = "C8:ZZ8"
                Const cStrText = "FY"

                Dim objWs As Worksheet
                Dim objCell As Range
                Dim vntSheets As Variant
                Dim intCounter As Integer

                vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

                For intCounter = LBound(vntSheets) To UBound(vntSheets)

                Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

                For Each objCell In objWs.Range(cStrRange)

                If objCell.Value <> cStrText Then
                objCell.EntireColumn.Hidden = True
                End If

                Next

                Next

                End Sub


                I have Excel 2003, so I used Z8 instead of ZZ8 which is obvious in the following picture.



                enter image description here



                Sub FY_SHOW222()

                Dim objWs As Worksheet
                Dim vntSheets As Variant
                Dim intCounter As Integer

                vntSheets = Array("Sheet1", "Sheet2", "Sheet4")

                For intCounter = LBound(vntSheets) To UBound(vntSheets)

                Set objWs = ThisWorkbook.Worksheets(vntSheets(intCounter))

                objWs.Columns.EntireColumn.Hidden = False

                Next

                End Sub


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 0:41









                VBasic2008VBasic2008

                2,7192415




                2,7192415






























                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439161%2fhiding-columns-in-vba-excel%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







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin