+ Reply to Thread
Results 1 to 8 of 8

VBA to hide column if another column contains a string?

  1. #1
    Registered User
    Join Date
    12-20-2016
    Location
    United States
    MS-Off Ver
    2016
    Posts
    40

    VBA to hide column if another column contains a string?

    Hi everyone, I'm hitting a roadblock with what is probably a simple fix, but I cannot figure out the syntax for it.

    If any cell in column "I" contains the strong "Printer", I would like columns J thru M to be unhidden. If no cell in column "I" contains the string "Printer", then I would like the columns to be hidden.

    I currently am using the following VBA, which unfortunately only works if a single cell (in this case, "I3") contains the string "Printer", rather than any cell in the entire column of "I":

    Please Login or Register  to view this content.
    I feel this needs to be some sort of "for each column in" type statement, but I'm unable to find any syntax that has worked.

    Also, my column I contains a drop-down list. Is there any way to make the columns hide/unhide as soon as I choose "Printer" or something else from the list, rather than having to move to another cell for the columns to hide/unhide? Thanks everyone!!!!!

    -Andrew

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: VBA to hide column if another column contains a string?

    Try:
    Please Login or Register  to view this content.
    If you want a macro that you can run manually, try:
    Please Login or Register  to view this content.
    Last edited by Mumps1; 10-06-2017 at 12:05 PM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA to hide column if another column contains a string?

    Hi,

    I'd simplify this and create a 'check' cell that counts the number of occurrences of "Printer" in column I.
    i.e. =
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Name the check cell say "check" then the macro is just

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    12-20-2016
    Location
    United States
    MS-Off Ver
    2016
    Posts
    40

    Re: VBA to hide column if another column contains a string?

    Quote Originally Posted by Mumps1 View Post
    Try:
    Please Login or Register  to view this content.
    If you want a macro that you can run manually, try:
    Please Login or Register  to view this content.
    Thanks for the help Mumps! I tried the first macro so it would be dynamic/automatic, but nothing happened when I changed the value to or from "Printer" in any row in of column "I". I replaced my exiting macro with yours, of course. Does the macro work for you? It looks like it should do exactly what I want, from the best I can tell.




    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    I'd simplify this and create a 'check' cell that counts the number of occurrences of "Printer" in column I.
    i.e. =
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Name the check cell say "check" then the macro is just

    Please Login or Register  to view this content.
    Thanks Richard! I am able to get my helper cell (the one with the COUNTIF formula) to change from 0 to 1 when selecting "Printer" in any cell within column I, but the other columns will not hide nor unhide when I manipulate the value in column I. I did also define a name of "check" for the helper cell, by the way.





    Since neither of your guys' formulas are hiding the columns I'm wondering if there's something wrong on my end. I've restarted Excel several times (usually helps when macros stop working in a file) and no joy. I had a coworker try from his computer and it didn't work for him either. Anything I might be missing? Thanks again guys!

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA to hide column if another column contains a string?

    Quote Originally Posted by AGrayson84 View Post
    Thanks Richard! I am able to get my helper cell (the one with the COUNTIF formula) to change from 0 to 1 when selecting "Printer" in any cell within column I, but the other columns will not hide nor unhide when I manipulate the value in column I. I did also define a name of "check" for the helper cell, by the way.
    Would you clarify what you mean by 'selecting printer in any cell within a row'.

    Your original request simply said if ANY cell in the column CONTAINS the word Printer. Which is why I suggested you use a count of the word Printer in the column so that if there was at least one word Printer then the check cell would be a number >0 and hence the macro would hide/unhide the columns that you suggested.

    Maybe it's time to upload the workbook, tell us how you use it, what you change and what you expect to happen.

  6. #6
    Registered User
    Join Date
    12-20-2016
    Location
    United States
    MS-Off Ver
    2016
    Posts
    40

    Re: VBA to hide column if another column contains a string?

    Hi Richard, thanks again for the help.

    I think the method you used works just fine-- if the count of the word "Printer" in column "I" exceeds 0, then the VBA should unhide the columns. That's what I understand of your VBA and it makes total sense to me, I just have not been able to get the columns to automatically hide nor unhide with that. The counter does seem to increase from 0 just fine as I choose "Printer" from my drop-down list in multiple cells within column "I", so that part is working just fine, it's just that when I go from 0 to anything greater than 0, or from anything greater than 0 to 0, the columns will not hide/unhide dynamically.

    I went back to the original macro I was using when I started this thread, and as long as I don't have "Printer" in cell I3 the macro hides the columns just fine, and when I change that value to "Printer" it unhides the columns perfectly. Obviously the only problem with that macro is it can only look in I3 instead of all of column I.

    Your method should absolutely work, so I'm wording if the If Range("check") > 0 Then portion is not recognizing the name I defined ("check") within the Name Manager, on the cell with the COUNTIF formula, since the counter itself is working, and since you're using the same syntax to hide and unhide the cells, which works just fine in my original macro I posted.

    Unfortunately our workstations at work prevent us from uploading any sort of files, so I can upload it when I get home later tonight if you'd like to review it. Thanks again Richard, I really appreciate it!

  7. #7
    Registered User
    Join Date
    12-20-2016
    Location
    United States
    MS-Off Ver
    2016
    Posts
    40

    Re: VBA to hide column if another column contains a string?

    Quote Originally Posted by Richard Buttrey View Post
    Would you clarify what you mean by 'selecting printer in any cell within a row'.
    Oh and sorry, to clarify that, what I mean is I actually have a drop-down list used in all of column "I", and the goal is: if "Printer" is the value of any cell within column "I", then unhide columns J thru M.

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA to hide column if another column contains a string?

    Yes upload the workbook when you get a chance.

    Just a thought but VBA code is usually case sensitive. What do you have in column I? Printer, PRINTER or printer. In any case alway use code like

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    so that whatever the case LCase converts it to lower case. Of course non of that is relevant if you're using a cell called "check" and evaluating a >0 result.

+ 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] I need to hide column f or column g based on text in cell b2
    By A440 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-03-2015, 11:16 AM
  2. Replies: 5
    Last Post: 12-14-2014, 10:59 PM
  3. VBA Hide row if cells in column F contains text string from cell A1
    By bone343 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-16-2014, 03:09 PM
  4. [SOLVED] VBA macro that hide and unhide column to respective rows based on first column value
    By janine6192 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-14-2013, 06:21 PM
  5. [SOLVED] count the number of times each string appears in a column and make a summary column
    By intothewild in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-04-2012, 08:02 AM
  6. [SOLVED] Hide show column selected column based on value
    By ola7mat in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-07-2012, 02:44 PM
  7. Replies: 5
    Last Post: 01-18-2012, 08:33 AM

Tags for this Thread

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