+ Reply to Thread
Results 1 to 5 of 5

Hide Columns Not Specifically Listed

  1. #1
    Registered User
    Join Date
    08-15-2018
    Location
    Calgary, Canada
    MS-Off Ver
    2013
    Posts
    3

    Hide Columns Not Specifically Listed

    Hello All,
    New to the forum and a bit of a hack with VBA (still learning...)
    Any help would be greatly appreciated with my issue.

    Essentially, I am trying to write code to hide all columns where the header title is not specifically excluded (required to be visible).
    I have the below that I have found and modified for my purposes. It works okay aside that it hides many extra empty columns...
    Can't seem to figure out why?

    Eg.
    Headers are in columns "A to Q" (but could be more hence why I want it to be dynamic)
    Visible columns should be "A to G" after running macro, and they are, but it is also hiding columns "H to DT" ???

    Please Login or Register  to view this content.
    Thanks!

  2. #2
    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: Hide Columns Not Specifically Listed

    Hi and welcome to the forum,

    For stuff like this I generally use a helper row (say row 1) with a formula that determines whether the column should be hidden. e.g. in A1 copied across

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


    Then a macro like
    Please Login or Register  to view this content.
    You are picking up lots of extra columns becuase of the UsedRange property. Even when you've deleted stuff in columns or rows so that the entire column/row is blank, Excel still remembers the last column & row you used. This will persist until you save the workbook.

    Incidentally there's a school of thought that says integer variables are converted to long variables by VBA so you may as well declare them as long in the first place and avoid VBA having to spend time doing it. Not that you'd notice a few extra milliseconds but if true, and I've no reason to doubt it, the idea is a sound one.
    Last edited by Richard Buttrey; 08-15-2018 at 06:36 PM.
    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.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Hide Columns Not Specifically Listed

    Re: "many extra empty columns"
    Does that mean Columns without a header?

  4. #4
    Registered User
    Join Date
    08-15-2018
    Location
    Calgary, Canada
    MS-Off Ver
    2013
    Posts
    3

    Re: Hide Columns Not Specifically Listed

    Thank you for the quick reply!
    I didn't know that about the integer variables or the UsedRange property...
    I will try modifying as you have suggested.
    Thanks again!

  5. #5
    Registered User
    Join Date
    08-15-2018
    Location
    Calgary, Canada
    MS-Off Ver
    2013
    Posts
    3

    Re: Hide Columns Not Specifically Listed

    Thank you,
    Yes, I am getting additional columns with no header hidden...
    As Richard noted above, I believe it is due to me using the UsedRange.

+ 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. Display names listed in multiple columns.
    By BrentRobinson in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-06-2018, 10:05 AM
  2. How to identify in a matrix if an X is listed in two columns of data
    By neim0003 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-25-2016, 01:51 PM
  3. how to ensure 4 columns amounts are listed once per identifier
    By accelatexcel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-30-2015, 01:50 AM
  4. Replies: 4
    Last Post: 03-31-2015, 11:40 AM
  5. How to compare 2 columns that are listed in ranked order
    By slughorn in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 04-16-2013, 12:16 AM
  6. [SOLVED] writing the columns a number is listed in
    By David in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-11-2005, 08:06 PM
  7. identify numbers which are listed in two columns.
    By the_kane in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-08-2005, 03: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