Display only columns with certain text

AlexCHI

New Member
Joined
Sep 28, 2017
Messages
19
Hi all,

Please teach me how I can display only columns with certain text in Excel 2007? There are a lot of help on filtering rows but I am hoping to create either a toggle button or set up a reference cell (say "B4") along with a VBA to display columns with selected days only.

For example, method #1 : the user click the toggle button labeled "Sunday" or method #2 : the user inputs "Sunday" in "B4". Then only column T and column AA and all the Sunday columns will show.

Thank you for any help you can think of.


Excel 2007
STUVWXYZAA
6AM Attendance2
7PM Attendance2
8AM & PM Attendance1
9Jan-1Jan-2Jan-3Jan-4Jan-5Jan-6Jan-7Jan-8
10Days PresentSunMonTueWedThuFriSatSun
111AM & PM
121PM
131AM
14
Main Screen
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Re: How to display only columns with certain text

In this example we input "Sun" only columns "T" an "AA" would be visible.
And the Day "Sun" will always be in row (10)

So in this example we would not see column "S" because there is no "Sun" in row(10) of column "S"
Is this correct? If not please give more details
 
Last edited:
Upvote 0
Re: How to display only columns with certain text

Hi My Aswer Is This,

Thank you for your reply. In response to your post, please refer to the following for clarification:

1. Yes, the days (Mon, Tue, Wed, Thu, Fri, Sat, Sun) will always be in row 10
2. Actually, I would like to keep column A:S intact without being impacted by the VBA filter.
3. The range to apply the VBA filter to show/hide the specified columns (either through the method of clicking the toggle button or inputting the day in cell B4) is column T:NT.

Hope the above makes sense... Thank you in advance for your help!
 
Upvote 0
Re: How to display only columns with certain text

With #2 , in cell B4 is your input "Sun" or "Sunday"? The following works for either short hand day name or full name day name (i.e. 3 letters or full word) entered into B4:
Code:
Sub HideCols()

    Dim rngB4   As Range
    Dim rng     As Range
    Dim LC      As Long
    Dim y       As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        Set rngB4 = .Cells(4, 2)
        LC = .Cells(10, .Columns.count).End(xlToLeft).column
        .Cells(10, 20).Resize(, LC - 19).EntireColumn.Hidden = False
        If Len(rngB4.Value) > 0 And InStr("sunday,monday,tuesday,wednesday,thursday,friday,saturday", LCase$(rngB4.Value)) > 0 Then
            For y = 20 To LC
                If .Cells(10, y).Value = rngB4.Value Or Left$(rngB4.Value, 3) = .Cells(10, y).Value Then
                    If Not rng Is Nothing Then
                        Set rng = Union(rng, .Cells(10, y))
                    Else
                        Set rng = .Cells(10, y)
                    End If
                End If
            Next y
        End If
        
        If Not rng Is Nothing Then
            rng.EntireColumn.Hidden = True
            Set rng = Nothing
        End If
        
        Set rngB4 = Nothing
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Re: How to display only columns with certain text

Try this:
Will look in Range "B4" for day

Code:
Sub Hide_Columns()
Application.ScreenUpdating = False
Dim i As Long
Dim Choice As String
Choice = Range("B4").Value
Columns.Hidden = False
    For i = 384 To 20 Step -1
        If Cells(10, i).Value <> Choice Then
        
        Columns(i).Hidden = True
        Else: Columns(i).Hidden = False
        End If
    Next
Application.ScreenUpdating = True
MsgBox "Done"
End Sub
 
Upvote 0
Re: How to display only columns with certain text

@M.I.A.T There's ambiguity whether the input value in B4 is the full name of the day, versus the screenshot which shows T10:AA10 with shortened names, however, thanks to your code, spotted code I suggested was doing the opposite of the request, correction highlighted and assumption is to check for 3 letter day name:
Rich (BB code):
Sub HideCols()


    Dim rngB4   As Range
    Dim rng     As Range
    Dim LC      As Long
    Dim y       As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        Set rngB4 = .Cells(4, 2)
        LC = .Cells(10, .Columns.count).End(xlToLeft).column
        .Cells(10, 20).Resize(, LC - 19).EntireColumn.Hidden = False
        If Len(rngB4.Value) > 0 And InStr("sunday,monday,tuesday,wednesday,thursday,friday,saturday", LCase$(rngB4.Value)) > 0 Then
            For y = 20 To LC
                If Left$(rngB4.Value, 3) <> .Cells(10, y).Value Then
                    If Not rng Is Nothing Then
                        Set rng = Union(rng, .Cells(10, y))
                    Else
                        Set rng = .Cells(10, y)
                    End If
                End If
            Next y
        End If
        
        If Not rng Is Nothing Then
            rng.EntireColumn.Hidden = True
            Set rng = Nothing
        End If
        
        Set rngB4 = Nothing
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Re: How to display only columns with certain text

The User should put a Data validation list in Range("B4") and in in Row(10). This way there is no ambiguity. Thanks.
@M.I.A.T There's ambiguity whether the input value in B4 is the full name of the day, versus the screenshot which shows T10:AA10 with shortened names, however, thanks to your code, spotted code I suggested was doing the opposite of the request, correction highlighted and assumption is to check for 3 letter day name:
Rich (BB code):
Sub HideCols()


    Dim rngB4   As Range
    Dim rng     As Range
    Dim LC      As Long
    Dim y       As Long
    
    Application.ScreenUpdating = False
    
    With ActiveSheet
        Set rngB4 = .Cells(4, 2)
        LC = .Cells(10, .Columns.count).End(xlToLeft).column
        .Cells(10, 20).Resize(, LC - 19).EntireColumn.Hidden = False
        If Len(rngB4.Value) > 0 And InStr("sunday,monday,tuesday,wednesday,thursday,friday,saturday", LCase$(rngB4.Value)) > 0 Then
            For y = 20 To LC
                If Left$(rngB4.Value, 3) <> .Cells(10, y).Value Then
                    If Not rng Is Nothing Then
                        Set rng = Union(rng, .Cells(10, y))
                    Else
                        Set rng = .Cells(10, y)
                    End If
                End If
            Next y
        End If
        
        If Not rng Is Nothing Then
            rng.EntireColumn.Hidden = True
            Set rng = Nothing
        End If
        
        Set rngB4 = Nothing
    End With
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Re: How to display only columns with certain text

Alex, can you confirm exactly what is in row 10 from column T onward?
Is it ..
- Directly entered text "Sun", "Mon" etc, or
- Formula returning text eg =TEXT(T9,"ddd"), or
- Formula returning a date but then custom formatted to show the day name. eg =T9 with custom format of "ddd", or
- Directly entered date with custom format eg 1-Jan-2017 with custom format of "ddd"
 
Upvote 0
Re: How to display only columns with certain text

M.I.A.T
The User should put a Data validation list in Range("B4") and in in Row(10). This way there is no ambiguity. Thanks.

This hasn't been stated anywhere either by OP or yourself.


or method #2 : the user inputs "Sunday" in "B4". Then only column T and column AA and all the Sunday columns will show.

The screen shot, clearly shows values of Sun, Mon, Tue etc, to me, this is NOT the same as Sunday in B4 hence both @
Peter_SSs and myself checking clarification on this. Thanks.
 
Last edited:
Upvote 0
Re: How to display only columns with certain text

Hi Peter and Jack,

My apologies of the ambiguity. Row 10 from column T onward is formula returning text, e.g. =TEXT(T9,"ddd").

Jack, really appreciate the two input options in B4! If possible, would like to keep both short hand and full name day names as inputted options for the user.

Much appreciated for all your help!
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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