简体   繁体   English

删除除Excel VBA范围以外的所有列

[英]Delete all except a range of columns excel VBA

I have hundreds of Columns in excel that I don't need. 我有数百个不需要的Excel专栏。 I have a range that I want to keep. 我有一个范围要保持。

At the minute I have 在那一刻我有

Sub DeleteClms ()
    Range("A:G,L:O").Delete
End Sub

Is there anyway to make this an opposite, in other languages I would simply put a =!. 无论如何,是否有相反的说法,在其他语言中,我只需输入=!。 I have tried putting <> in but I dont know where/how to put it into my code? 我曾尝试将<>放入,但不知道在哪里/如何将其放入代码中?

Thanks 谢谢

There is no Excel or VBA function for the Symetric Difference of the columns that I know of. 我知道的列的Symetric Difference没有Excel或VBA函数。

Here is a quick VBA function to get there. 这是到达此处的快速VBA功能。 Usage would be DeleteAllBut Range("A:C,H:Q") 用法将为DeleteAllBut Range("A:C,H:Q")

Sub DeleteAllBut(rngToKeep As Range)
    Dim ws As Worksheet
    Dim rngToDelete As Range
    Dim rngColi As Range

    'Number of columns used in worksheet
    Set ws = rngToKeep.Parent
    iCols = ws.UsedRange.Columns.Count

    FirstOne = True
    For i = 1 To iCols
        Set rngColi = Range("A:A").Offset(0, i - 1)
        If Intersect(rngToKeep, rngColi) Is Nothing Then
            If FirstOne Then
                Set rngToDelete = rngColi
                FirstOne = False
            Else
                Set rngToDelete = Union(rngColi, rngToDelete)
            End If
        End If
    Next i

    Debug.Print rngToDelete.Address & " was deleted from " & ws.Name
    rngToDelete.Delete


End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM