Toggle between Visible and Hidden Columns

  • Hello there


    A few years ago I contacted this forum for help with writing a simple VBA/Macro for an excel table. It is a training matrix with names in the first column and each course covering 2 columns with a Completed column and Due column. These words (Completed and Due) were written on row 5 and so I needed to create a toggle that would hide the Completed column and then another to hide the Due column to make viewing easier. This is what I came up with:


    Code
    Sub Hide_Columns_Toggle()
    Dim c As Range
     For Each c In Rows("5:5").Cells
       If c.Value = "Completed" Then
          c.EntireColumn.Hidden = c.EntireColumn.Hidden
       End If
     Next c
    End Sub


    It has been a long time since I looked at this and I do not use VBA normally so I am lost as to how to amend this.


    I now need to add a third column to each course, so I may have the word completed in two columns but with other words (ie instead of saying Completed the column may say Online Completed on row 6). Can you tell me how to amend this VBA so that I can:


    a) toggle the hide column if the column CONTAINS the word Completed, and

    b) toggle the hide column if the column contains word A or word B (where A and B are the headings not yet decided. Gives me flexibilty if I want to change the heading but still use the toggle)


    I hope I have explained that clearly. It is just a simple VBA, but I am lost without the help of people who use this all the time. Lotus 123 I could manage, VBA is a mystery to me!!


    Thank you so much for any help offered.


    T

    Edited once, last by Carim: Added Code Tags ().

  • Hi,


    Just out of curiosity ... does the macro you have posted generate your expected result ?

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Since you apparently want to Toggle between Visible and Hidden Columns, would suggest testing the following macro


    Do Not Forget : Not ....

    Code
    Sub Hide_Columns_Toggle()
    Dim c As Range
     For Each c In Range("A5:Z5")
       If c.Value = "Completed" Then c.EntireColumn.Hidden = Not c.EntireColumn.Hidden
     Next c
    End Sub


    Hope this will help :)

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

    Edited once, last by Carim: Do NOT Forget : NOT ().

  • Thank you for your reply Carim.


    Yes, the code I wrote works fine every time, but now that I am adding another column instead of having just "Completed" as a column header, it might say "Online Completed". It would be good to have a code that hides the column if it CONTAINS the word Completed, or any other word I chose. Option b) is just incase the new column does not have the word completed in it but I want to hide it at the same time as the completed column.


    I don't know much about VBA, so forgive me if I have this wrong, but doesn't the code you have given hide the column if the cell says "Completed" rather than contains the word completed. I thought it would be like Value = "*Completed*" or something, but I am a novice. A little knowledge can get me into a lot of trouble! Really appreciate your quick reply.


    T

  • You can test following modification

    Code
    Sub Hide_Columns_Toggle()
    Dim c As Range
     For Each c In Range("A5:Z5")
       If c.Value = "*" & "Completed" & "*" Then c.EntireColumn.Hidden = Not c.EntireColumn.Hidden
     Next c
    End Sub

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Carim

    Changed the title of the thread from “Help with VBA to hide columns” to “Toggle between Visible and Hidden Columns”.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!