Macro to hide columns by Header Names

zalik22

Board Regular
Joined
Dec 14, 2010
Messages
111
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am looking to hide columns by header names. The columns I would like to hide are "name", "date" and "age".

Thanks!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Generic solution:

Code:
Private Const FirstRow = 1 ' The row to check
Public Sub HideNameDateAge()

' Hide "Name", "Date" and "Age" columns
HideColumns True, "Name", "Date", "Age"

End Sub
Public Sub ShowNameDateAge()

' Show "Name" and "Date" columns
HideColumns False, "Name", "Date"

End Sub
Public Sub HideColumns(hideColumn As Boolean, ParamArray columnHeaders() As Variant)

Dim lastCol As Long
Dim thisCol As Long
Dim columnName As Variant

' Find the last column
lastCol = Cells(FirstRow, Columns.Count).End(xlToLeft).Column

' Look at all columns
For thisCol = 1 To lastCol
    ' Look at the names we were passed
    For Each columnName In columnHeaders
        ' Check if it matches
        If Cells(FirstRow, thisCol).Value = columnName Then
            ' Show / Hide as appropriate
            Cells(FirstRow, thisCol).EntireColumn.Hidden = hideColumn
        End If
    Next columnName
Next thisCol

End Sub

WBD
 
Upvote 0
Try this:

Code:
Sub Hide_Me()
'"name", "date" and "age"
Dim Lastcolumn As Long
Lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To Lastcolumn
        If Cells(1, i).Value = "name" Or Cells(1, i).Value = "date" Or Cells(1, i).Value = "age" Then Columns(i).Hidden = True
    Next
End Sub
 
Last edited:
Upvote 0
Great! Thanks so much!
Try this:

Code:
Sub Hide_Me()
'"name", "date" and "age"
Dim Lastcolumn As Long
Lastcolumn = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To Lastcolumn
        If Cells(1, i).Value = "name" Or Cells(1, i).Value = "date" Or Cells(1, i).Value = "age" Then Columns(i).Hidden = True
    Next
End Sub
 
Upvote 0
Is there a way to reference a column, I want to do some formatting with a column header "User ID" in column D. I know
Columns("D:D").Select
works, but was trying to use

Columns("User ID").Select
but getting an error. Thanks!
 
Upvote 0
How about
Code:
Sub Fluff15_8()
    
    Dim Col As Long
    Col = Rows(1).Find("User ID").Column
    Columns(Col).Select


End Sub
 
Upvote 0
Getting an error, this is what I have:
Dim Col As Long
Col = Rows(1).Find("User ID").Column
Columns(Col).Select
Selection.ColumnWidth = 75
Selection.WrapText = True
 
Upvote 0
What error are you getting & what line does it highlight if you click debug?
 
Upvote 0
You can but you need to create a table (Select your table then INSERT>TABLE) and give it a name (default is Table1) in the 'DESIGN' tab that pops up when you select any cell within your table

So for example if I have a table named Table1 and in it I have a column titled "Name" I can reference it like this:

Code:
Range("Table1[Name]").EntireColumn.Hidden = True
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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