VBA Auto delete unwanted Columns

usui

Board Regular
Joined
Apr 20, 2020
Messages
55
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi Masters, can anyone help me create a VBA code that can help remove unwanted columns start from column H..

I want a VBA that I can enter just the column header that I want to remain in the sheets.

1640202265030.png


Sample: I want to enter " Tea " in the input box then all other columns with different headers will be remove starting from column H only.

I hope someone can help with this problem.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Assuming that your headers are in row 1, try this:
VBA Code:
Sub MyDeleteColumns()

    Dim colHdr As String
    Dim lc As Long
    Dim c As Long
    
    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 1
    lc = Cells(1, 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(1, c).Value <> colHdr Then Columns(c).Delete
    Next c
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Assuming that your headers are in row 1, try this:
VBA Code:
Sub MyDeleteColumns()

    Dim colHdr As String
    Dim lc As Long
    Dim c As Long
   
    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 1
    lc = Cells(1, 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(1, c).Value <> colHdr Then Columns(c).Delete
    Next c
   
    Application.ScreenUpdating = True
   
End Sub

Can this also work on multiple sheets or workbook?
 
Upvote 0
Assuming that your headers are in row 1, try this:
VBA Code:
Sub MyDeleteColumns()

    Dim colHdr As String
    Dim lc As Long
    Dim c As Long
   
    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 1
    lc = Cells(1, 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(1, c).Value <> colHdr Then Columns(c).Delete
    Next c
   
    Application.ScreenUpdating = True
   
End Sub

I have tried this but it also removes the column header that I entered.. headers are located at Row 3..i tried changing it to row 3 but still the same

if possible is there a way make those unwanted columns to be hide instead of deleted??
 
Upvote 0
Assuming that your headers are in row 1, try this:
VBA Code:
Sub MyDeleteColumns()

    Dim colHdr As String
    Dim lc As Long
    Dim c As Long
   
    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 1
    lc = Cells(1, 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(1, c).Value <> colHdr Then Columns(c).Delete
    Next c
   
    Application.ScreenUpdating = True
   
End Sub

It's working now, yehey..the only problem is that it only works on 1 sheet... do you have a code to make it work on multiple sheets or in 1 workbook??
 
Upvote 0
It's working now, yehey..the only problem is that it only works on 1 sheet... do you have a code to make it work on multiple sheets or in 1 workbook??
Just wrap it in a loop that goes through all worksheets, i.e.
VBA Code:
Sub MyDeleteColumns()

    Dim ws As Worksheet
    Dim colHdr As String
    Dim lc As Long
    Dim c As Long
    
    Application.ScreenUpdating = False
    
'   Prompt for name of column
    colHdr = InputBox("Enter name of column that you would like to keep")

'   Loop through all worksheets
    For Each ws In Worksheets
        ws.Activate
        
'       Find last column in row 1
        lc = Cells(1, 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(1, c).Value <> colHdr Then Columns(c).Delete
        Next c
        
    Next ws
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
And per forum rules, please do NOT start new threads on the same subject!
Note that you might not always get immediate replies (I do sleep sometimes), so you may need to be a little patient.
 
Upvote 0
Just wrap it in a loop that goes through all worksheets, i.e.
VBA Code:
Sub MyDeleteColumns()

    Dim ws As Worksheet
    Dim colHdr As String
    Dim lc As Long
    Dim c As Long
   
    Application.ScreenUpdating = False
   
'   Prompt for name of column
    colHdr = InputBox("Enter name of column that you would like to keep")

'   Loop through all worksheets
    For Each ws In Worksheets
        ws.Activate
       
'       Find last column in row 1
        lc = Cells(1, 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(1, c).Value <> colHdr Then Columns(c).Delete
        Next c
       
    Next ws
   
    Application.ScreenUpdating = True
   
End Sub
OMG..you are a life saver Joe4..thank you so much for the great help
 
Upvote 0
And per forum rules, please do NOT start new threads on the same subject!
Note that you might not always get immediate replies (I do sleep sometimes), so you may need to be a little patient.
this is noted. thank you
 
Upvote 0
You are welcome.
Glad we could help.
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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