Hiding colums

EXCELGPS

New Member
Joined
Aug 31, 2017
Messages
13
Hi,
I have a spreadsheet that has columns with headings (F-AA), I want to hide all the columns where the heading doesn't match whats in cell D28.

Easy way of doing this please?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Re: Hidng colums

Try:
Code:
Sub HideCols()
    
    Dim x               As Long
    Dim rng             As Range
    Const HeaderRow     As Long = 1
    
    Application.ScreenUpdating = False

    Cells(HeaderRow, 6).Resize(, 22).EntireColumn.Hidden = False

    For x = 6 To 27
        If Cells(HeaderRow, x).Value <> Cells(28, 4).Value Then
            If rng Is Nothing Then
                Set rng = Cells(HeaderRow, x)
            Else
                Set rng = Union(rng, Cells(HeaderRow, x))
            End If
        End If
    Next x
        
    rng.EntireColumn.Hidden = True
    
    Application.ScreenUpdating = True
    Set rng = Nothing
        
End Sub
 
Last edited:
Upvote 0
Re: Hidng colums

You are running the code from the same raw data starting point and not after the macro has been run once or more?

No, when you change the value in D28 from the drop down list the columns need to switch and re-hide to match the new value in D28, the value in D28 will be changed multiple times in one session.
 
Last edited:
Upvote 0
Re: Hidng colums

It's working I think, I added this to the beginning of your code in post #12 , I'll test it properly now and report back, thanks for you help on this.


Code:
Columns("E:AB").Select
    Selection.EntireColumn.Hidden = False
 
Upvote 0
Re: Hidng colums

I realised what the mistake was hence the code looks different.

Code:
Cells(HeaderRow, 6).Resize(, 22).EntireColumn.Hidden = False
If HeaderRow = 1 then Cells(HeaderRow, 6) = F1 (F 6th letter of alphabet)

Cells(HeaderRow, 6).Resize(,22) is F1:AA1

If you need E:AB, change that line of code to:
Code:
Cells(HeaderRow, 5).Resize(, 24).EntireColumn.Hidden = False
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

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