guest1
guest1

Reputation: 295

How to delete columns of a range Excel VBA

I have a range named "Start" located at cell "I21". And I have another range named "End" located at cell "Q21". I want to write a code to delete all the columns between them. In other words, I want to delete columns J,K,L,M,N,O,P completely. Here is the code I have:

with ThisWorkbook.sheets("Sheet1")
    'unprotect sheet
    .Columns(.Range("Start").Column+1 & ":" & .Range("End").Column-1).Select
     Selection.Delete Shift:xlLeft
End with 

when it comes to the first line .Columns... it gives me an error as undefined application. please help,

Upvotes: 3

Views: 33593

Answers (3)

Fandango68
Fandango68

Reputation: 4898

You don't have to specify a row reference...

ActiveSheet.Range(, MyColumn).EntireColumn.Delete

Where 'myColumn' is an arbitrary reference to a column number or anything you want.

Upvotes: 0

user1448329
user1448329

Reputation: 1

    Dim xlsRange As Excel.Range

    xlsRange = xlsSheet.Range("i2", "i10")

    xlsRange.EntireColumn.Delete()

Upvotes: -1

shahkalpesh
shahkalpesh

Reputation: 33484

Range(Range("start").Offset(,1), Range("end").Offset(,-1)).EntireColumn.Delete  

Upvotes: 7

Related Questions