VBA code help! Removing unwanted columns using inputbox

usui

Board Regular
Joined
Apr 20, 2020
Messages
55
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi Masters,

I would like to ask help about a vba code that can help me removing unwanted columns base on the data..and too just enter it in a inputbox or pop up box with multiple data separate with comma.

1622018077186.png


here is a sample: i wanted to remove the columns in yellow, by just entering their data ( Power Source, Number of Lantern Heads, Integrated LE) like this or enter any data showing in the row1 in each columns.

and also to make it work on the others sheets as well since we have multiple sheets in 1 workbook.

please help me anyone?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Maybe this? It works on active sheet at the time it runs. If you want it to work on other sheets then how do you want to feed the code the sheet names?

VBA Code:
Sub RemoveCols()

Dim ibox As String, arr As Variant, i As Long, col As Variant

ibox = InputBox("Enter column headers to delete separated by commas", "Headers")

If Len(ibox) = 0 Then
    Exit Sub
Else
    arr = Split(ibox, ",")
    For i = LBound(arr) To UBound(arr)
        With ActiveSheet
            col = Application.Match(Trim(arr(i)), .Rows(1), 0)
            If IsNumeric(col) Then
                .Columns(col).Delete Shift:=xlToLeft
            End If
        End With
    Next
End If
    
End Sub
 
Upvote 0
Maybe this? It works on active sheet at the time it runs. If you want it to work on other sheets then how do you want to feed the code the sheet names?

VBA Code:
Sub RemoveCols()

Dim ibox As String, arr As Variant, i As Long, col As Variant

ibox = InputBox("Enter column headers to delete separated by commas", "Headers")

If Len(ibox) = 0 Then
    Exit Sub
Else
    arr = Split(ibox, ",")
    For i = LBound(arr) To UBound(arr)
        With ActiveSheet
            col = Application.Match(Trim(arr(i)), .Rows(1), 0)
            If IsNumeric(col) Then
                .Columns(col).Delete Shift:=xlToLeft
            End If
        End With
    Next
End If
   
End Sub
Hi Steve thank you for this and it works, but if you can make the other sheets delete the same, i just need to automatically delete it to all sheets after clicking OK on the input box
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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