Results 1 to 7 of 7

Thread: Hide columns that contain 0 value

  1. #1
    VBAX Newbie
    Joined
    Oct 2024
    Location
    Saudi Arabia
    Posts
    1
    Location

    Hide columns that contain 0 value

    Hi Everyone,
    Need you support to solve my issue and reduce the manual work through VBA.
    I have big sheet have more that 300 columns (all the cells contain formulas), some columns are have values and some have zero values (but it changeable next day),
    I need the VBA to search if the column has zero value and take action to hide that column. and if possible to unhide them if they have values.
    Thanks
    ...

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    So if Sum of a column = 0, hide the column?
    Column headers are first row and data starts on second row?

    Check out https://stackoverflow.com/questions/...at-are-empty-0
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Banned VBAX Regular
    Joined
    Aug 2023
    Posts
    9
    Location
    Quote Originally Posted by DJBoY View Post
    Hi Everyone,
    Need you support to solve my issue and reduce the manual work through VBA. I have big sheet have more that 300 columns (all the cells contain formulas), some columns are have values and some have zero values (but it changeable next day),
    I need the VBA to search if the column has zero value and take action to hide that column. and if possible to unhide them if they have values.
    Thanks
    You can use a VBA macro to hide columns in an Excel worksheet that contain only zero values and unhide them when they have non-zero values. The code loops through each column, checks for non-zero values, and hides or unhides accordingly. To implement, open the VBA editor, insert a new module, and paste the provided code, then run the macro.

  4. #4
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    623
    Location
    Paste the following into a regular module :

    Option Explicit
    
    'If the entire Column is empty :
    Sub HideCol()
        Dim N As Long, wf As WorksheetFunction, M As Long
        Dim i As Long, j As Long
        N = Columns.Count
        M = Rows.Count
        Set wf = Application.WorksheetFunction
        Application.ScreenUpdating = False
        For i = 26 To 1 Step -1 '<<<---------------------------- Change 26 (Col Z) to reflect max columns possible
            If wf.CountBlank(Columns(i)) <> M Then Exit For
        Next i
        For j = i To 1 Step -1
            If wf.CountBlank(Columns(j)) = M Then
                Cells(1, j).EntireColumn.Hidden = True
            End If
        Next j
        Application.ScreenUpdating = True
    End Sub
    Create a command button that is attached to the above macro.
    Attached Files Attached Files
    Last edited by Aussiebear; 04-03-2025 at 01:51 PM.

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    Quote Originally Posted by DJBoY View Post
    Hi Everyone,
    Need you support to solve my issue and reduce the manual work through VBA.
    I have big sheet have more that 300 columns (all the cells contain formulas), some columns are have values and some have zero values (but it changeable next day),
    I need the VBA to search if the column has zero value and take action to hide that column. and if possible to unhide them if they have values.
    Thanks
    Do you mean if the sum of the column = 0 or if one of the entries is 0?


    So col A = 1,2,3,-3,-2,-1 sum = 0

    or

    Col A = 1,2,0,3,2,1 contains 0
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Banned VBAX Newbie
    Joined
    Feb 2025
    Posts
    2
    Location
    Quote Originally Posted by DJBoY View Post
    Hi Everyone,
    Need you support to solve my issue and reduce the manual work through VBA.
    I have big sheet have more that 300 columns (all the cells contain formulas), some columns are have values and some have zero values (but it changeable next day),
    I need the VBA to search if the column has zero value and take action to hide that column. and if possible to unhide them if they have values.
    Thanks
    You can use the following VBA code to hide columns with zero values ​​and show them again when there are other values.
    Last edited by Aussiebear; 07-08-2025 at 03:46 AM. Reason: Edited out a spam link.

  7. #7
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    @Sophian, there is no code in your post.

    OP hasn't responded on this 9-month old post, so likely has abandoned.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Posting Permissions

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