Solved

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

Posted on 2011-03-21
3
198 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now