调整VBA以删除所有符合条件的列

时间:2013-08-13 15:41:56

标签: excel vba

如果单元格为Column,我有一个在第2行移动的宏并删除blank

删除部分有效,但如果有N个空格单元相邻,我必须运行N次才能删除所有列,是否可以删除一遍中的所有列,因为我不知道有多少空白细胞可能彼此相邻。

由于

Sub delete-columns()
For Each cell In Range(Cells(2, 1), Cells(2, ActiveSheet.UsedRange.Columns.count))
    If cell.Value = "" Then cell.EntireColumn.Delete xlToRight
Next cell
End Sub

1 个答案:

答案 0 :(得分:2)

这将删除活动工作表中包含第2行空白的所有列:

Sub DeleteColsWithBlanks()
Dim ws As Excel.Worksheet

Set ws = ActiveSheet
With ws
    .Range(.Cells(2, 1), .Cells(2, .Columns.Count)).SpecialCells(xlCellTypeBlanks).EntireColumn.Delete
End With
End Sub