Delete unselected columns

llane23

New Member
Joined
Jan 31, 2023
Messages
10
Office Version
  1. 365
Platform
  1. MacOS
I got this code from another question about deleting columns, but instead of deleting the selected columns, I would like to delete every other column if that's possible.

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 i
End If
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Assuming that headers start from cell A1, this could be a solution:
Code:
Option Explicit
Sub DontRemoveTheseCols()
    Dim ibox   As String, arr As Variant, i As Long
    Dim x      As Long, ToDelete As Boolean, col As Long
    ibox = InputBox("Enter column headers not to delete separated by commas", "Headers")
    If Len(ibox) = 0 Then
        Exit Sub
    Else
        With ActiveSheet
            arr = Split(ibox, ",")
            col = Cells.Find(What:="*", After:=.[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
            For x = col To 1 Step -1
                ToDelete = False
                For i = LBound(arr) To UBound(arr)
                    If UCase(Trim(arr(i))) = UCase(Trim(Cells(1, x))) Then
                        ToDelete = True
                    End If
                Next i
                If ToDelete = False Then .Columns(x).Delete Shift:=xlToLeft
            Next x
        End With
    End If
End Sub
 
Upvote 0
Solution
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,019
Members
449,280
Latest member
Miahr

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