我在excel中有数百个我不需要的列。我有一个我想保留的范围。
我的那一刻
Sub DeleteClms ()
Range("A:G,L:O").Delete
End Sub
反正是否有相反的意思,在其他语言中,我只想放一个=!。 我试过把<>但是我不知道在哪里/如何将它放入我的代码中?
由于
答案 0 :(得分:0)
我所知道的列的对称差异没有Excel或VBA函数。
这是一个快速的VBA功能。用法为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