Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple columns using column numbers

Tags:

excel

vba

Just wanted to share as I had huge amount of trouble looking for ways to do this online and have finally gotten it through series of trial and error.

Sheet1.Range(Cells(1, 11), Cells(1, 100)).EntireColumn.Delete

This deletes columns 11 to 100.

like image 635
Jay Avatar asked Sep 18 '13 16:09

Jay


People also ask

How do I delete multiple columns at once?

If you need to remove multiple columns that are next to each other at once, select the first column of the batch – click on the left button of the mouse, then hold and drag through all the columns you want to delete.

How do you delete multiple columns in Excel with conditions?

To select entire columns, either click on the first column heading and drag to the side until you reach the number of columns you want to delete or select the first cell of the column, press and hold the Ctrl and Shift keys and press the Down key, then release the Ctrl key (still holding the Shift key) and press the ...

How do I delete a bunch of columns in Excel?

To delete unwanted rows and columns in your spreadsheet, just simply highlight the row or column by clicking the marker on top of the column or to the left of the row, just right-click it and then click delete. Hope this helps you.

How do I delete multiple columns in Excel VBA?

If we want to delete multiple columns, we cannot enter columns. We need to reference the columns by column headers, i.e., alphabets. This will delete the column from A to D, i.e., the first 4 columns. Like this, we can use the “Delete Column” method in VBA to delete the particular columns.


1 Answers

More ways

Deleting consecutive columns like 1 - 100

Sub Sample()
    With Sheet1
        'A:CV
        .Columns(ReturnName(1) & ":" & ReturnName(100)).Delete Shift:=xlToLeft
    End With
End Sub

'~~> Returns Column Name from Col No
Function ReturnName(ByVal num As Integer) As String
    ReturnName = Split(Cells(, num).Address, "$")(1)
End Function

Deleting non consecutive columns like 1, 3, 5

Sub Sample()
    With Sheet1
        'A:A,C:C,E:E
        .Range( _
                ReturnName(1) & ":" & ReturnName(1) & "," & _
                ReturnName(3) & ":" & ReturnName(3) & "," & _
                ReturnName(5) & ":" & ReturnName(5) _
               ).Delete Shift:=xlToLeft
    End With
End Sub

Function ReturnName(ByVal num As Integer) As String
    ReturnName = Split(Cells(, num).Address, "$")(1)
End Function

'**Another way**

Sub Sample()
    Dim Rng As Range

    With Sheet1
        Set Rng = Union(.Columns(1), .Columns(3), .Columns(5))
    End With

    Rng.Delete Shift:=xlToLeft
End Sub
like image 152
Siddharth Rout Avatar answered Nov 15 '22 08:11

Siddharth Rout