删除区域中的空白列或零列



这就是我需要的:范围说列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

最新更新