使用列号删除多个列

时间:2013-09-18 16:56:35

标签: excel excel-vba vba

只是想分享,因为我在网上寻找方法遇到了大量的麻烦,并最终通过一系列的反复试验得到了它。

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

这会删除第11至100列。

2 个答案:

答案 0 :(得分:6)

更多方式

删除连续的列,例如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

删除非连续的列,例如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

答案 1 :(得分:5)

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