+ Reply to Thread
Results 1 to 4 of 4

How to hide and unhide columns with the same button

Hybrid View

Allan91 How to hide and unhide... 07-25-2023, 01:31 PM
JEC. Re: How to hide and unhide... 07-25-2023, 02:10 PM
Allan91 Re: How to hide and unhide... 07-25-2023, 02:24 PM
skywriter Re: How to hide and unhide... 07-25-2023, 02:29 PM
  1. #1
    Registered User
    Join Date
    12-19-2020
    Location
    Turkey
    MS-Off Ver
    2019
    Posts
    19

    How to hide and unhide columns with the same button

    Hi All,

    I am trying to write a code where I can hide all columns that contains "0" in a specific cell in each column. The values are linked to another sheet and they are dynamic as they will change as new data arrives. I have managed to do this bit but some of the cells which previously were "0" are bound to change and hence I will no longer want them to be hidden anymore.

    So I tried to embed an extra line of code which commands to unhide all columns and then run the above mentioned code so that everything will be refreshed. My code does not seem to work though; probably because I am not ending the first command properly and telling VBA to run the next code or it just loops the first one. It must be something very simple, however, I am not certain at all.

    Below is the code I have tried and failed with.

    Any help will be much appreciated.

    Many thanks in advance!

    
    Sub Refresh_Distribution()
    Dim p As Range
    
        For Each p In Range("A15:AM15").Cells
            If p.Value = "0" Then
                p.EntireColumn.Hidden = True
            
                 End If
           Next p
        
    End Sub
    
    Sub Unhide_and_Hide_Relevant_Columns()
    
        Columns.EntireColumn.Hidden = False
               
        Refresh_Portfolio_Distribution "this one is the name of the first code"
    
        MsgBox "Fund Distibution Table is refreshed."
        
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: How to hide and unhide columns with the same button

    Hi try

    Sub jec()
     Dim it
     For Each it In Range("A15:AM15")
        it.Columns.Hidden = it = "0"
     Next
    End Sub
    Last edited by JEC.; 07-25-2023 at 02:13 PM.

  3. #3
    Registered User
    Join Date
    12-19-2020
    Location
    Turkey
    MS-Off Ver
    2019
    Posts
    19

    Re: How to hide and unhide columns with the same button

    This works. Thank you!

  4. #4
    Forum Expert skywriter's Avatar
    Join Date
    06-09-2014
    Location
    USA
    MS-Off Ver
    365 Version 2409
    Posts
    2,789

    Re: How to hide and unhide columns with the same button

    The values are linked to another sheet and they are dynamic as they will change as new data arrives.
    You can run code automatically when the values change so you don't have to push a button.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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