Results 1 to 7 of 7

Thread: [RESOLVED] Excel VBA: Macro to hide columns not working

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2014
    Posts
    74

    Resolved [RESOLVED] Excel VBA: Macro to hide columns not working

    I have a macro which is supposed to provide a view of my database, however, when I run the macro, it will not work.

    What I'm trying to get the macro to do is hide the following columns:
    • G through M
    • P
    • T through Z


    but when I run the macro it hides G through M, and O through Z. I have tried to break the macro down into several smaller macros and "call" them in a master macro for that series of macros, but I continue to get the same results.

    Here is the master macro:
    Code:
    Sub View_Fiscal()
    '
    ' Macro1 Macro
    '
    
        Application.ScreenUpdating = False
        
        Call Unhide_Contra_Info
        Call VF_1
        Call VF_2
        Call vf_3
        
        Range("a1").Select
    
    End Sub
    
    Private Sub VF_1()
    
        Columns("G:M").Select
        Selection.EntireColumn.Hidden = True
        
        Range("a1").Select
        
    End Sub
    
    Private Sub VF_2()
    
        Columns("P:P").Select
        Selection.EntireColumn.Hidden = True
        
        Range("a1").Select
        
    End Sub
    
    Private Sub vf_3()
    
        Columns("T:Z").Select
        Selection.EntireColumn.Hidden = True
    
        Range("a1").Select
        
    End Sub

  2. #2
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Excel VBA: Macro to hide columns not working

    if i comment out the call to "contra..." it does exactly what you are trying to do.

  3. #3
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Excel VBA: Macro to hide columns not working

    But you don't need to select the columns first, so more like this:

    Code:
    Sub hideCols()
        Dim ws As Worksheet
        
        Set ws = ActiveSheet
        ws.Range("g1:m1").EntireColumn.Hidden = True
        ws.Range("p1").EntireColumn.Hidden = True
        ws.Range("t1:z1").EntireColumn.Hidden = True
        Set ws = Nothing
    End Sub

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Excel VBA: Macro to hide columns not working

    Quote Originally Posted by vbfbryce View Post
    But you don't need to select the columns first, so more like this:

    Code:
    Sub hideCols()
        Dim ws As Worksheet
        
        Set ws = ActiveSheet
        ws.Range("g1:m1").EntireColumn.Hidden = True
        ws.Range("p1").EntireColumn.Hidden = True
        ws.Range("t1:z1").EntireColumn.Hidden = True
        Set ws = Nothing
    End Sub
    Bryce you work to hard!
    Code:
     ActiveSheet.Range("g:m, p:p, t:z").EntireColumn.Hidden = True
    Or is it that you get paid by the line?

  5. #5
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Excel VBA: Macro to hide columns not working

    I DO get paid by the line item (when doing "project management" here at work!)!


  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2014
    Posts
    74

    Re: Excel VBA: Macro to hide columns not working

    Thanks guys, I'm not sure why my original code wasn't working, I thought they were doing the same thing... do you have any idea why that was?? My original code was performing an entirely different operation than what I was calling for. Maybe it was because there was no Dim'ing?

    Either way, I married the two of your codes together, ran it, and it works perfectly.

    Here's what I used:
    Code:
    Sub hidecols()
    
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
        Application.ScreenUpdating = False
        
        Call Unhide_Contra_Info
            
        ActiveSheet.Range("g:m,p:p,t:z").EntireColumn.Hidden = True
            
        Range("a1").Select
    
    End Sub
    Thanks again.

  7. #7
    New Member
    Join Date
    Jul 2015
    Posts
    11

    Re: [RESOLVED] Excel VBA: Macro to hide columns not working

    The this useful example file for the topic can be reviewed : Excel Vba Column Hiding-Unhiding With Horizontal Form



    In this template userform, module, class module was used.

    Source for example file

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width