0
votes

Am wanting to have a macro/button that toggles between hiding and unhiding select columns. I had something similar that hid/unhid rows but can't figure this one out.

Sub PrintToggle()
With ActiveSheet
    With Range("C:C,E:E,H:H,L:O,U:U,AA:AA,AF:AF,AI:AK").Columns
        If .EntireColumn.Hidden Then
            .EntireColumn.Hidden = False
        Else
            .EntireColumn.Hidden = True
        End If
    End With
End With

End Sub

This use to at least hide the columns but after saving and re-opening, it now hides the entire sheet. I can't get it to stop doing this let along unhide the same columns. Thanks!

1
The .Columns is redundant but otherwise works fine for me. - BigBen
This is frustrating. I restarted and had only that file open. Ran it and it's still hiding everything with no way to unhide. I have to keep opening the last saved version. - Neuner
You're missing a . in front of Range, or just get rid of the With ActiveSheet btw. - BigBen
If I do this from VBE Debug.Print ActiveSheet.Range("A:A,C:C").Address, I get Application-defined or object-defined error(1004). But when I do this Debug.Print Range("A:A,C:C").Address I get Method 'Range' of object '_Global' failed(1004). But I figured out that I need to use the semi-colon (;) instead of the comma. - VBasic2008
No Repo, this works as expected for me. - chris neilsen

1 Answers

1
votes

loop over a cell in each row worked for me

Sub PrintToggle()

    Dim r As Range
    
    For Each r In ActiveSheet.Range("C1,E1,H1,L1:O1,U1,AA1,AF1,AI1:AK1")
        If r.EntireColumn.Hidden Then
            r.EntireColumn.Hidden = False
        Else
            r.EntireColumn.Hidden = True
        End If
    Next r
End Sub