vba 使用列号删除多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18878062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:38:40  来源:igfitidea点击:

Delete multiple columns using column numbers

excelexcel-vbavba

提问by Jay

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

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

This deletes columns 11 to 100.

这将删除第 11 到 100 列。

回答by Siddharth Rout

More ways

更多方式

Deleting consecutive columns like 1 - 100

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

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

回答by Jay

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