删除范围内的空白或零列

这是我需要的:范围说列F:F,G:G,K:K。 如果单元格为零或仅为这些特定列为空,请删除整个列。 没有任何行动,如果有任何其他的价值。

下面给出的是我现在使用的。 但是这会删除不应该被删除的其他列。

LastRow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row LastColumn = Sheets("Sheet1").Cells(9, Columns.Count).End(xlToLeft).Column lastcol_name_1 = Replace(Cells(1, LastColumn).Address(False, False), "1", "") 'to find letter of last column With Sheets("Sheet1") For i = 1 To LastColumn lastcol_name_1 = Replace(Cells(1, i).Address(False, False), "1", "") 'to find letter of last column If(lastcol_name_1=“K” or lastcol_name_1=”L” or lastcol_name_1=”M”) then 'write the column name here If Evaluate("sum(countif(" & .Range(.Cells(10, i), _ .Cells(LastRow, i)).Address & ",{"""",0}))") = LastRow - 10 + 1 Then _ .Columns(i).Delete Endif Next i 

如果我正确地理解了你的话,下面的列应该删除“F”,“G”和“K”列,如果它们是空的,把剩下的列移到左边 – 假设你想在这里做什么。 我已经尽可能多地保留了自己的代码:

 Sub main(): LastColumn = Sheets("Sheet1").Cells(9, Columns.Count).End(xlToLeft).Column lastcol_name_1 = Replace(Cells(1, LastColumn).Address(False, False), "1", "") For i = LastColumn To 1 Step -1 lastcol_name_1 = Replace(Cells(1, i).Address(False, False), "1", "") If (lastcol_name_1 = "F" Or lastcol_name_1 = "G" Or lastcol_name_1 = "K") Then If Application.CountA(Columns(i).EntireColumn) = 0 Then Columns(i).Delete Shift:=xlToLeft End If End If Next i End Sub