?
Solved

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

Posted on 2011-03-21
3
Medium Priority
?
205 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

765 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