Hiding/Showing every other column Macro Code

lbstateguy

New Member
Joined
May 26, 2011
Messages
3
Hi there,
I have a spreadsheet where I want to hide/show empty columns. Problem is that I have everyother column hidden already. I wrote a code that works great, but when I run a macro that adds columns (hidden and showing), the new macro does not work. Can anyone help?

This is the code I have
Code:
Sub HideCol()
    Dim cl As Range, rTest As Range
    Set rTest = Range("F75:AH75", Range("F75:AH75").End(xlToRight))
    For Each cl In rTest
        If Not cl.Value > 0 Then
            cl.EntireColumn.Hidden = True
        End If
    Next cl
End Sub
Sub UnHideCols()
Dim cl As Range
For Each cl In Sheets("Budget").Range("F75, H75, J75, L75, N75, P75, R75, T75, V75, X75, Z75, AB75, AD75, AF75, AH75, AJ75, AK75, AL75, AM75")
  If UCase(cl) = "0" Then
      cl.EntireColumn.Hidden = False
  Else: cl.EntireColumn.Hidden = False
  End If
Next cl
End Sub
[endcode]
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
normally this code
Set rTest = Range("F75:AH75", Range("F75:AH75").End(xlToRight)) should work if there are no blanks in rows.

try this more general code


set rtest=range(range("A1:A11"),cells(1,columns.Count).end(xltoleft).end(xldown))
modify the cells addresses to suit you
 
Upvote 0
Hi there,
Thanks for your insight. The macro I have works great but the problem I run into occurs after I run another macro to add (not hide/show) columns. I get an error on the following line:

Sub HideCol()
Dim cl As Range, rTest As Range
Set rTest = Range("F75:AZ75", Range("F75:AZ75").End(xlToRight))
For Each cl In rTest
If Not cl.Value > 0 Then
cl.EntireColumn.Hidden = True
End If
Next cl
End Sub

Can you help me clear this error?

Thank you!
 
Upvote 0
Hi there,
I figured out what the problem was. Sheet was set to protect itself after the first macro ran. I had to add a code to unprotect the sheet before 2nd macro ran.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top