简体   繁体   English

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

[英]Tweak VBA to delete all Columns that fit criteria

I have a macro that moves through row 2 and deletes a Column if a cell is blank . 我有一个宏在第2行移动,如果单元格为blank ,则删除一个Column

The delete parts work, but I have to run it N times to remove all the columns if there are N blank cells adjacent to one another, is it possible to delete all columns in one pass as I do not know how many blank cells might be next to one another. 删除部分有效,但如果有N个空格单元相邻,我必须运行N次以删除所有列,是否可以删除一遍中的所有列,因为我不知道有多少空白单元格可能是彼此相邻。

Thanks 谢谢

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

This will delete all columns in the active sheet with blanks in row 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

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

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