Solved

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

Posted on 2011-03-21
3
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

733 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