Is it possible to filter columns?

Ruben Schmitz

New Member
Joined
Aug 31, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Dear reader,

I am busy making a schedule in Excel, now I have a question about filtering in Excel. I need to filter out the grey columns. But if I use the filter by colour and set the fitler on grey the row will be filtered out.
Does anyone know how to filter columns?

It is also oke if it is possible in a other way? maybe by using VBA or something

Thank you!

Best regard,

Ruben Schmitz

1601887021548.png
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The way of filtering you are looking for is not possible. This is by Excel's design (database oriënted). In your situation the use of VBA is inevitable.
Because of your use of buttons on the worksheet, I assume that you are familiar with the use of the VBE. In writing the code below I made the following assumptions:
- column Q and onwards are to be affected;
- columns were colored in their entirety before the colors of some particular cells within those columns were changed;
The effect of the first assumption can be easily overruled by changing the cStartCol constant in the code.
Changing the effect of the second assumption needs a less minor modification, so if that's required let me know.

This goes in the worksheet module of PlanningProject:
VBA Code:
Private Sub Worksheet_Activate()
    ' retrieve toggle state
    Call GreyColumns(Init, Me)
End Sub


This goes in a standard module:
VBA Code:
Option Explicit

Private Const cStartCol As String = "Q"     ' << first column (and onwards) on sheet 'ProjectPlanning'
Private Const cColor    As Long = 14277081  ' << color to act on

Private bVisible        As Boolean
Private oSheet          As Worksheet

Public Enum ColumnState
    Init
    Hide
    Show
End Enum

Public Sub ShowColor()
    Const cMsg  As String = "Interior Color of cell @addr@ is: "
    Dim sMsg    As String
    With Selection
        If .CountLarge > 1 Then
            MsgBox "Please select a single cell ..."
        Else
            sMsg = Replace(cMsg, "@addr@", .Address & " on sheet " & .Parent.Name) & .Interior.color
            MsgBox sMsg
        End If
    End With
End Sub

Public Sub GreyColumnsToggle()
    If bVisible Then
        Call GreyColumns(Hide, oSheet)
    Else
        Call GreyColumns(Show, oSheet)
    End If
End Sub

Public Sub GreyColumns(ByVal argAction As ColumnState, ByRef argWs As Worksheet)
    Dim rng  As Range
    Dim c    As Range

    With argWs
        ' determine sheets most bottom row within target columns
        Set rng = .Range(cStartCol & "1", Cells(1, .UsedRange.Column + UBound(.UsedRange.Formula, 2) - 1).EntireColumn)
        Set rng = Application.Intersect(rng, .Cells(.Rows.Count, 1).EntireRow)
    End With

    Select Case argAction

        Case Init
            Set oSheet = argWs
            bVisible = True
            For Each c In rng.Columns
                If c.Hidden Then
                    bVisible = False
                    Exit For
                End If
            Next c

        Case Hide
            For Each c In rng
                If c.Interior.color = cColor Then c.EntireColumn.Hidden = True
            Next c
            bVisible = False

        Case Show
            For Each c In rng
                If c.Interior.color = cColor Then c.EntireColumn.Hidden = False
            Next c
            bVisible = True

        Case Else
            ' do nothing
    End Select
End Sub

The code uses a random color as conditional color of which the number is assigned to the cColor constant, which easily can be changed. The ShowColor procedure can be used to obtain the number of the color you are using to distinguish those particular columns from the columns which must stay displayed. This color number has to be assigned to the cColor constant in the code.
The GreyColumnsToggle procedure switches between displaying the colored columns and hiding them. This procedure can be assigned to a button on your worksheet.
 
Upvote 0
Hi Ruben, unfortunately I have overlooked something :eek: so you might experience a run-time error.
Replace the above GreyColumns procedure with the one below and all should be fine.
VBA Code:
Public Sub GreyColumns(ByVal argAction As ColumnState, ByRef argWs As Worksheet)
    Dim oWsA As Worksheet
    Dim oWsT As Worksheet
    Dim rng  As Range
    Dim c    As Range
    
    If Not argWs Is Nothing Then
        With argWs
            ' determine sheets most bottom row within target columns
            Set rng = .Range(cStartCol & "1", Cells(1, .UsedRange.Column + UBound(.UsedRange.Formula, 2) - 1).EntireColumn)
            Set rng = Application.Intersect(rng, .Cells(.Rows.Count, 1).EntireRow)
        End With
        Select Case argAction
            Case Init
                Set oSheet = argWs
                bVisible = True
                For Each c In rng.Columns
                    If c.Hidden Then
                        bVisible = False
                        Exit For
                    End If
                Next c
            Case Hide
                For Each c In rng
                    If c.Interior.color = cColor Then c.EntireColumn.Hidden = True
                Next c
                bVisible = False
            Case Show
                For Each c In rng
                    If c.Interior.color = cColor Then c.EntireColumn.Hidden = False
                Next c
                bVisible = True
            Case Else
                ' do nothing
        End Select
    Else
        ' on opening Workbook oSheet has to be initialized
        With Application
            Set oWsA = ActiveSheet
            .EnableEvents = True
            For Each oWsT In oWsA.Parent.Sheets
                If oWsT.Name <> oWsA.Name Then
                    .ScreenUpdating = False
                    oWsT.Activate
                    oWsA.Activate
                    .ScreenUpdating = True
                    Call GreyColumnsToggle
                    Exit For
                End If
            Next oWsT
        End With
    End If
End Sub
 
Upvote 0
Hi GWTeb,

thank you for replying, my excues for the late reply, i have been busy with something else, but now i have the time to focus on this.

First I am not really experienced with VBA and macro's

I have put the first vba code in the correct worksheet, also I have tried your second correct VBA code, there is only one problem. when I try to make a form control button, there is no option to choose the macro greycolumns ( see picture knipsel 2 vba code) . when I delete the "(ByVal argAction As ColumnState, ByRef argWs As Worksheet)" it is possible to choose greycolumns, but then when i try to run it i get a error in the first row ( picture knipsel 1 vba code).

I do not know if i need to put the macro in a form button or that it can run by him self.

Also something else, in the picture of my first messages you can see the cells above the grey collums, the orange cells with 5 okt 2020 and 12 okt 2020 ect. are cells that are merges & center. maybe this can be a problem when you try to filter collums with the vba code.

i hope you can help me with this
 

Attachments

  • Knipsel 2 vba code.PNG
    Knipsel 2 vba code.PNG
    215.4 KB · Views: 3
  • Knipsel 1 vba code.PNG
    Knipsel 1 vba code.PNG
    156.5 KB · Views: 3
Upvote 0
Hi Ruben,
I have got the impression that you partially pasted the code. However, you were supposed to use all code from my post #2, with the exception of the GreyColumns procedure, which you should copy from post #3. Although the GreyColumns procedure does all the work, this procedure is called indirectly and with the use of two mandatory arguments (parameters):
1) the type of action
2) the worksheet to which that action relates.

This way you can use the same procedure for multiple worksheets if desired. Due to the necessary arguments this procedure cannot be assigned to a button directly and is therefore not shown in the Assign Macro dialog (Macro toewijzen aan object).
The GreyColumns procedure is called when the worksheet to be affected is activated (for initialization purposes).
The GreyColumns procedure is also called by the GreyColumnsToggle procedure, the latter can be assigned to a custom button on your worksheet.

The code uses a random color as conditional color of which the number is assigned to the cColor constant (on the third line), which easily can be changed.
The ShowColor procedure can be used to obtain the number of the color you are using to distinguish those particular columns from the columns which must stay displayed. Select a single cell and invoke this procedure (Ribbon > Developer tab (Ontwikkelaars) > Macros > ShowColor macro > Run (Uitvoeren)). The obtained color number has to be assigned manually to the cColor constant in the code (so replacing the random number 14277081).

I don't think merged cells will affect the operation of the code. Should this nevertheless prove to be the case, do unmerge these worksheet ranges and use text alignment instead (Celeigenschappen > Uitlijning tab > Tekstuitlijning Horizontaal > Centreren over selectie), see attached images.

So for completeness...

This goes in the module of the (or each) desired worksheet:

VBA Code:
Private Sub Worksheet_Activate()
    ' retrieve toggle state
    Call GreyColumns(Init, Me)
End Sub


This goes in a standard module:
VBA Code:
Option Explicit

Private Const cStartCol As String = "Q"     ' << first column (and onwards) on sheet 'ProjectPlanning'
Private Const cColor    As Long = 14277081  ' << color to act on

Private bVisible        As Boolean
Private oSheet          As Worksheet

Public Enum ColumnState
    Init
    Hide
    Show
End Enum

Public Sub ShowColor()
    Const cMsg  As String = "Interior Color of cell @addr@ is: "
    Dim sMsg    As String
    With Selection
        If .CountLarge > 1 Then
            MsgBox "Please select a single cell ..."
        Else
            sMsg = Replace(cMsg, "@addr@", .Address & " on sheet " & .Parent.Name) & .Interior.Color
            MsgBox sMsg
        End If
    End With
End Sub

Public Sub GreyColumnsToggle()
    If bVisible Then
        Call GreyColumns(Hide, oSheet)
    Else
        Call GreyColumns(Show, oSheet)
    End If
End Sub

Public Sub GreyColumns(ByVal argAction As ColumnState, ByRef argWs As Worksheet)
    Dim oWsA As Worksheet
    Dim oWsT As Worksheet
    Dim rng  As Range
    Dim c    As Range
    
    If Not argWs Is Nothing Then
        With argWs
            ' determine sheets most bottom row within target columns
            Set rng = .Range(cStartCol & "1", Cells(1, .UsedRange.Column + UBound(.UsedRange.Formula, 2) - 1).EntireColumn)
            Set rng = Application.Intersect(rng, .Cells(.Rows.Count, 1).EntireRow)
        End With
    
        Select Case argAction
    
            Case Init
                Set oSheet = argWs
                bVisible = True
                For Each c In rng.Columns
                    If c.Hidden Then
                        bVisible = False
                        Exit For
                    End If
                Next c
    
            Case Hide
                For Each c In rng
                    If c.Interior.Color = cColor Then c.EntireColumn.Hidden = True
                Next c
                bVisible = False
    
            Case Show
                For Each c In rng
                    If c.Interior.Color = cColor Then c.EntireColumn.Hidden = False
                Next c
                bVisible = True
    
            Case Else
                ' do nothing
        End Select
    Else
        
        With Application
            Set oWsA = ActiveSheet
            .EnableEvents = True
            For Each oWsT In oWsA.Parent.Sheets
                If oWsT.Name <> oWsA.Name Then
                    .ScreenUpdating = False
                    oWsT.Activate
                    oWsA.Activate
                    .ScreenUpdating = True
                    Call GreyColumnsToggle
                    Exit For
                End If
            Next oWsT
        End With
    End If
End Sub
 

Attachments

  • ScreenShot059.png
    ScreenShot059.png
    29.2 KB · Views: 5
  • ScreenShot060.png
    ScreenShot060.png
    6.4 KB · Views: 4
Upvote 0
Hi GWteB,

I have tried you VBA scrips and it seems that it is working, only there are 3 problems.

the first problem is that the grey rows are made grey by conditional formatting, because of that the script reads the original colour (white - code: 16777215).

the second problem is that it is only hiding the columns when the whole column or a big part of it has the colour that is named in the VBA script. sow the range is to big, the range should be between row 14 and 251 from colomn Q.

the third problem is with the merged cells, I have tried your other optie, but when a column is hidden the information in the cell disappears (see picture) the right is with a merged cell and on the left is with the other option. I have tried to put the information in in all 5 cells and than merged or the other option, but this is also not working. maybe it is nog possible to fix this.

I hope you can help me with these 3 things and otherwise thank you a lott!
 

Attachments

  • Knipsel.PNG
    Knipsel.PNG
    7.8 KB · Views: 1
Upvote 0
Hi Gwteb,

I maybe have a other idea, the columbs are be filters by 5 rows (see picture), when there is a 1 in the cell the cell will be made grey by conditional formatting. maybe it is a option to make the columns hide by these 5 rows? then you do not have to problems about the range and the conditional formatting in my last post
 

Attachments

  • Knipsel.PNG
    Knipsel.PNG
    7.6 KB · Views: 1
Upvote 0
@Ruben Schmitz, you may have already noticed, I've been off the air for a while. The following may help. In my previous code, replace any occurrence of c.Interior.Color with c.DisplayFormat.Interior.Color so the conditional formatting is taken into account. Let me know how far you've progressed.
 
Upvote 0

Forum statistics

Threads
1,215,840
Messages
6,127,214
Members
449,369
Latest member
JayHo

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