• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA code to hide filtered columns (simple table 5 rows / 6 columns wide)

Hi ..

Can you please check this one too..

https://dl.dropboxusercontent.com/u/78831150/Excel/FilterEvent.xlsm

Code:
Private Sub Worksheet_Calculate()
  FilterEvent
End Sub
Sub FilterEvent()
Dim AllN As Boolean
With Range("A1").CurrentRegion
  .Columns.Hidden = False
  If ActiveSheet.FilterMode Then
  For i = 2 To .Columns.Count
  AllN = True
  For Each cell In .Columns(i).Rows.Offset(1).Resize(.Rows.Count - 1).SpecialCells(12)
  If cell <> "N" Then
  AllN = False
  Exit For
  End If
  Next cell
  If AllN Then .Columns(i).Hidden = True
  Next i
  End If
End With
End Sub
 
Hi

If you put a count at the bottom of your table the code would be easier to apply.

Code:
Option Explicit
 
Sub HideIt()
    Dim rng As Range
    For Each rng In Range("B12:F12")
        If rng = 4 Then rng.EntireColumn.Hidden = Not (rng.EntireColumn.Hidden)
    Next
End Sub
 
Sub UnHideIt()
    Range("B12:F12").EntireColumn.Hidden = False
End Sub

File attached to show workings.

Take care

Smallman
 

Attachments

  • HideorUnhide.xlsm
    31.3 KB · Views: 5
Hi

If you put a count at the bottom of your table the code would be easier to apply.

Code:
Option Explicit

Sub HideIt()
    Dim rng As Range
    For Each rng In Range("B12:F12")
        If rng = 4 Then rng.EntireColumn.Hidden = Not (rng.EntireColumn.Hidden)
    Next
End Sub

Sub UnHideIt()
    Range("B12:F12").EntireColumn.Hidden = False
End Sub

File attached to show workings.

Take care

Smallman

Thanks for the reply
 
Hi ..

Can you please check this one too..

https://dl.dropboxusercontent.com/u/78831150/Excel/FilterEvent.xlsm

Code:
Private Sub Worksheet_Calculate()
  FilterEvent
End Sub
Sub FilterEvent()
Dim AllN As Boolean
With Range("A1").CurrentRegion
  .Columns.Hidden = False
  If ActiveSheet.FilterMode Then
  For i = 2 To .Columns.Count
  AllN = True
  For Each cell In .Columns(i).Rows.Offset(1).Resize(.Rows.Count - 1).SpecialCells(12)
  If cell <> "N" Then
  AllN = False
  Exit For
  End If
  Next cell
  If AllN Then .Columns(i).Hidden = True
  Next i
  End If
End With
End Sub


this one works great - many thanks
 
Back
Top