+ Reply to Thread
Results 1 to 13 of 13

Code to hide columns based on value of another column

  1. #1
    Forum Contributor Saeber4777's Avatar
    Join Date
    06-06-2014
    Location
    United States
    MS-Off Ver
    365 Enterprise
    Posts
    159

    Code to hide columns based on value of another column

    I was wondering if someone could help me with a simple code that could:

    If column K2:k466 is blank or says "No" then columns L & M are hidden. If it says "Yes" it is not hidden.

    Also,

    If Column N2:N466 is blank or says "No" then Columns O,P,Q,R are hidden. If it says "Yes" they are not hidden.

    Finally,

    If Column V2:V466 has a zip code (or number) then columns T,U,W,X,Y,Z,AA,AB,AC are present. If there is no value in any of V2:V466 then they are hidden.


    Thank you for your help.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Code to hide columns based on value of another column

    For columns K and N, if any cell in the range given is blank or No, then the whole columns are hidden? I'm confused by your request. Can you clarify with specifics and examples. Same for Column V.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Contributor Saeber4777's Avatar
    Join Date
    06-06-2014
    Location
    United States
    MS-Off Ver
    365 Enterprise
    Posts
    159

    Re: Code to hide columns based on value of another column

    Yes, if a value that = "Yes" is placed in K then L & M are visible. If no value or the value "No" is placed there, the columns are hidden. So any value that would be placed in cells K2:k466 that = "Yes" make L & M visible. Same for the others.

    Column V2:V466 are cells that zip codes go in. When a zip code goes in any of those cells, I want their associated columns to display. Otherwise, they are hidden. This is to keep clutter down because I am collecting large amounts of information.

    I'm not sure how to clarify more than that.
    Last edited by Saeber4777; 07-31-2014 at 08:59 PM.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Code to hide columns based on value of another column

    Try this code:

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor Saeber4777's Avatar
    Join Date
    06-06-2014
    Location
    United States
    MS-Off Ver
    365 Enterprise
    Posts
    159

    Re: Code to hide columns based on value of another column

    Shouldn't that go into private sub worksheet under Worksheet Change Event? Either way (general or Worksheet Change Event) it causing an error.
    Last edited by Saeber4777; 07-31-2014 at 11:21 PM.

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Code to hide columns based on value of another column

    I didn't create it as a worksheet change event. I created it as a straight module based upon your criteria as explained in post 1.

    How to install your new code
    1. Copy the Excel VBA code
    2. Select the workbook in which you want to store the Excel VBA code
    3. Press Alt+F11 to open the Visual Basic Editor
    4. Choose Insert > Module
    5. Edit > Paste the macro into the module that appeared
    6. Close the VBEditor
    7. Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)

    To run the Excel VBA code:
    1. Press Alt-F8 to open the macro list
    2. Select a macro in the list
    3. Click the Run button


    If you are looking for a worksheet change event, I will look at changing it up tomorrow, as it is bed time now on the East Coast and I am tired.

  7. #7
    Forum Contributor Saeber4777's Avatar
    Join Date
    06-06-2014
    Location
    United States
    MS-Off Ver
    365 Enterprise
    Posts
    159

    Re: Code to hide columns based on value of another column

    Yes, this would need to be automatic. I put a value in a cell, a column suddenly appears. Is this close?


    Please Login or Register  to view this content.
    Last edited by alansidman; 08-01-2014 at 08:25 PM. Reason: code tags added

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,895

    Re: Code to hide columns based on value of another column

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)


    Does the code you provided do what you want when you tested it?

  9. #9
    Forum Contributor Saeber4777's Avatar
    Join Date
    06-06-2014
    Location
    United States
    MS-Off Ver
    365 Enterprise
    Posts
    159

    Re: Code to hide columns based on value of another column

    Didn't know, sorry. How does that code look to you?

  10. #10
    Forum Contributor Saeber4777's Avatar
    Join Date
    06-06-2014
    Location
    United States
    MS-Off Ver
    365 Enterprise
    Posts
    159

    Re: Code to hide columns based on value of another column

    problem is, when I use yes in more than one of the columns it does not display everything correctly. It also does not hide the columns when there is no value in the trigger cells... the columns display until you put "No" in the trigger cell.

  11. #11
    Forum Contributor Saeber4777's Avatar
    Join Date
    06-06-2014
    Location
    United States
    MS-Off Ver
    365 Enterprise
    Posts
    159

    Re: Code to hide columns based on value of another column

    Here is the book with the code inside

    SampleWorkbook.xlsm

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code to hide columns based on value of another column

    What then would you do if there were both yes and no in column K
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  13. #13
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,613

    Re: Code to hide columns based on value of another column

    Please Login or Register  to view this content.
    Last edited by protonLeah; 08-02-2014 at 02:11 AM.
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Hide Columns based on level of access e.g. if "Read Only" Hide G:G
    By matrixpom in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-16-2013, 06:10 AM
  2. Code to show/hide columns based on fixed criteria
    By Gti182 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-07-2013, 03:39 AM
  3. Code to hide columns based on date criteria and insert another column with formulas
    By RandiLee in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-12-2012, 11:04 AM
  4. Macro to hide columns based on cell values in the column to be hidden
    By JCMus in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-15-2012, 06:06 PM
  5. VBA to put amounts in columns based on Code in column C.
    By mikeburg in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-25-2008, 12:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1