VBA Hide unwanted columns on multiple sheets or a workbook

Status
Not open for further replies.

usui

Board Regular
Joined
Apr 20, 2020
Messages
55
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi guys can anyone help me create a VBA code to hiding unwanted columns not needed, i want to enter the specific column header only that I want to remain on the sheets,

1640253445655.png


Sample: I want "Tea" (can be change anytime) to remain on all the sheets because this is a workbook, and the rest o the columns will be hidden so that only the Tea will remain
The search should only start on Column H, so meaning starting from column "H" all the columns aside from Tea will be hidden.


I hope someone can help me with this, and hoping to make it work on all the sheets in the workbook.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,

I created a sheet "main" in the workbook and in cell B2 typing the column heading (e.g. "tea") which we want to show.

The below code will compare the column headings of all sheets with the column heading available in B2 cell of main worksheet.

VBA Code:
Sub hideColumns()
Dim lCol As Integer
Dim shCount As Integer, sh As Integer, colno As Integer

shCount = ThisWorkbook.Sheets.Count

For sh = 1 To shCount
    If Sheets(sh).Name <> "main" Then
    
        lCol = Sheets(sh).Cells(1, Columns.Count).End(xlToLeft).Column
        For colno = 8 To lCol
            If Sheets(sh).Cells(1, colno) <> Sheets("main").Range("B2") Then
                Sheets(sh).Columns(colno).EntireColumn.Hidden = True
            End If
        Next
    End If
    
Next
End Sub


Book1
ABCD
1
2tea
3
4
5
6
7
8
9
10
11
main
 
Upvote 0
Hi,

I created a sheet "main" in the workbook and in cell B2 typing the column heading (e.g. "tea") which we want to show.

The below code will compare the column headings of all sheets with the column heading available in B2 cell of main worksheet.

VBA Code:
Sub hideColumns()
Dim lCol As Integer
Dim shCount As Integer, sh As Integer, colno As Integer

shCount = ThisWorkbook.Sheets.Count

For sh = 1 To shCount
    If Sheets(sh).Name <> "main" Then
   
        lCol = Sheets(sh).Cells(1, Columns.Count).End(xlToLeft).Column
        For colno = 8 To lCol
            If Sheets(sh).Cells(1, colno) <> Sheets("main").Range("B2") Then
                Sheets(sh).Columns(colno).EntireColumn.Hidden = True
            End If
        Next
    End If
   
Next
End Sub


Book1
ABCD
1
2tea
3
4
5
6
7
8
9
10
11
main

Not working for me. :( Headers are located at Row 3
 
Upvote 0
Hi,

I created a sheet "main" in the workbook and in cell B2 typing the column heading (e.g. "tea") which we want to show.

The below code will compare the column headings of all sheets with the column heading available in B2 cell of main worksheet.

VBA Code:
Sub hideColumns()
Dim lCol As Integer
Dim shCount As Integer, sh As Integer, colno As Integer

shCount = ThisWorkbook.Sheets.Count

For sh = 1 To shCount
    If Sheets(sh).Name <> "main" Then
   
        lCol = Sheets(sh).Cells(1, Columns.Count).End(xlToLeft).Column
        For colno = 8 To lCol
            If Sheets(sh).Cells(1, colno) <> Sheets("main").Range("B2") Then
                Sheets(sh).Columns(colno).EntireColumn.Hidden = True
            End If
        Next
    End If
   
Next
End Sub


Book1
ABCD
1
2tea
3
4
5
6
7
8
9
10
11
main

I have a working code here but it only works on 1 sheet..not on the other sheets..can you help me with this instead?


Sub MyDeleteColumns()
Dim ColumnsToHide As String, V As Variant
Dim xSh As Worksheet

Application.ScreenUpdating = False

' Prompt for name of column
colHdr = InputBox("Enter name of column that you would like to keep")

' Find last column in row 3
lc = Cells(3, Columns.Count).End(xlToLeft).Column

' Loop through all columns backwards and delete if not equal to entered in name
For c = lc To 8 Step -1
If Cells(3, c).Value <> colHdr Then Columns(c).Delete
Next

Application.ScreenUpdating = True

End Sub
 
Upvote 0
Hi, In my case it was row no 1. Check below.

VBA Code:
Sub hideColumns()
Dim lCol As Integer
Dim shCount As Integer, sh As Integer, colno As Integer

shCount = ThisWorkbook.Sheets.Count

For sh = 1 To shCount
    If Sheets(sh).Name <> "main" Then
   
        lCol = Sheets(sh).Cells(3, Columns.Count).End(xlToLeft).Column
        For colno = 8 To lCol
            If Sheets(sh).Cells(3, colno) <> Sheets("main").Range("B2") Then
                Sheets(sh).Columns(colno).EntireColumn.Hidden = True
            End If
        Next
    End If
   
Next
End Sub
 
Upvote 0
I have a working code here but it only works on 1 sheet..not on the other sheets..can you help me with this instead?


Sub MyDeleteColumns()
Dim ColumnsToHide As String, V As Variant
Dim xSh As Worksheet

Application.ScreenUpdating = False

' Prompt for name of column
colHdr = InputBox("Enter name of column that you would like to keep")

' Find last column in row 3
lc = Cells(3, Columns.Count).End(xlToLeft).Column

' Loop through all columns backwards and delete if not equal to entered in name
For c = lc To 8 Step -1
If Cells(3, c).Value <> colHdr Then Columns(c).Delete
Next

Application.ScreenUpdating = True

End Sub


Check below after few change in your code:

VBA Code:
Sub MyDeleteColumns()
Dim ColumnsToHide As String, V As Variant
Dim xSh As Worksheet, sh As Integer

Application.ScreenUpdating = False

' Prompt for name of column
colHdr = InputBox("Enter name of column that you would like to keep")


shCount = ThisWorkbook.Sheets.Count

For sh = 1 To shCount
    ' Find last column in row 3
    lc = Cells(3, Columns.Count).End(xlToLeft).Column
    
    ' Loop through all columns backwards and delete if not equal to entered in name
    For c = lc To 8 Step -1
        If Cells(3, c).Value <> colHdr Then Columns(c).Delete
    Next
Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Each sheet has more then 16,000 columns.
I would think hiding all these columns except for 1 in all the sheets in your workbook would be a large tasking
 
Upvote 0
Check below after few change in your code:

VBA Code:
Sub MyDeleteColumns()
Dim ColumnsToHide As String, V As Variant
Dim xSh As Worksheet, sh As Integer

Application.ScreenUpdating = False

' Prompt for name of column
colHdr = InputBox("Enter name of column that you would like to keep")


shCount = ThisWorkbook.Sheets.Count

For sh = 1 To shCount
    ' Find last column in row 3
    lc = Cells(3, Columns.Count).End(xlToLeft).Column
   
    ' Loop through all columns backwards and delete if not equal to entered in name
    For c = lc To 8 Step -1
        If Cells(3, c).Value <> colHdr Then Columns(c).Delete
    Next
Next
Application.ScreenUpdating = True

End Sub
Still the same only work on 1 sheet..so i need to do it manually each sheet...is their any code to be add so that we can only enter the text once and it will do it to all the sheets in the workbook?
 
Upvote 0
Hi, In my case it was row no 1. Check below.

VBA Code:
Sub hideColumns()
Dim lCol As Integer
Dim shCount As Integer, sh As Integer, colno As Integer

shCount = ThisWorkbook.Sheets.Count

For sh = 1 To shCount
    If Sheets(sh).Name <> "main" Then
  
        lCol = Sheets(sh).Cells(3, Columns.Count).End(xlToLeft).Column
        For colno = 8 To lCol
            If Sheets(sh).Cells(3, colno) <> Sheets("main").Range("B2") Then
                Sheets(sh).Columns(colno).EntireColumn.Hidden = True
            End If
        Next
    End If
  
Next
End Sub
it still did not work for me :( only on 1 sheet
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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