Solved

Macro; Hide columns where row <> 1 (excel 2003)

Posted on 2011-03-21
3
200 Views
Last Modified: 2012-05-11
Please open the excel file first.  The macro is running a bit strange:  scenario 1 should go all the way to column V, scenario 2 to column U, scenario 3 to column M, and scenario 4 should show all the columns in the workbook - try switching among the scenarios and see if this happens.  Scenarios can be changed en cell A9.

The macro below already hides the columns where row <> 1.  What could be added to the macro first "shows"/unhides all columns, then it hides the columns.  Because I will be chaning which columns will show and hide, I need to ensure that the macro "resets" itself.

Thanks,
JP     macro-hide-columns-not-equal-to-.xls
0
Comment
Question by:easycapital
  • 2
3 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35179164
Is this what you are trying?

Sub HideCols()
    Dim LastC As Long
    Dim Counter As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        .Cells.EntireColumn.Hidden = False
        
        Select Case .Range("A9").Value
        
        Case 1
            LastC = 22 '<~~ Col V
        Case 2
            LastC = 21 '<~~ Col U
        Case 3
            LastC = 13 '<~~ Col M
        Case 4
            '~~> ShowAll
            .Cells.EntireColumn.Hidden = False
            Exit Sub
        End Select
        
        For Counter = 1 To LastC
            .Cells(1, Counter).EntireColumn.Hidden = (.Cells(1, Counter) <> 1)
        Next
    End With
    
    Application.ScreenUpdating = True
    
    MsgBox "Done"
End Sub

Open in new window


Sid
0
 

Author Comment

by:easycapital
ID: 35179485
I "borrowed" one of your lines from above and added it to my existing code.

>>  .Cells.EntireColumn.Hidden = False

It seems to be working fine now.

Thanks,
JP



 
Sub HideCols()
    
    Dim LastC As Long
    Dim Counter As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        .Cells.EntireColumn.Hidden = False 'Show all columns

        LastC = .Cells(1, .Columns.Count).End(xlToLeft).Column
        .Range(.Cells(1, LastC + 1), .Cells(1, .Columns.Count)).EntireColumn.Hidden = True
        For Counter = 1 To LastC
            
            'if you meant "hide where cell <> 1" then use:
            
            .Cells(1, Counter).EntireColumn.Hidden = (.Cells(1, Counter) <> 1)
            
            ' if you meant "hide where cell does not contain 1" then use:
            
            '.Cells(1, Counter).EntireColumn.Hidden = (Not .Cells(1, Counter) Like "*1*")
            
        Next
    End With
    
    Application.ScreenUpdating = True
    
    MsgBox "Done"
    
End Sub

Open in new window

0
 

Author Closing Comment

by:easycapital
ID: 35179488
I borrowed one line, that I mentioned.
Thanks,
JP
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question