删除空白或空列使用较少的 Excel VBA 标题



我有以下VBA代码,它将删除完全空白的列。我想修改代码,以便如果前三行(标题(下方的范围为空,则将删除整列。有人可以帮忙吗?

Sub DeleteBlankColumns()
'Step1:  Declare your variables.
    Dim MyRange As Range
    Dim iCounter As Long
'Step 2:  Define the target Range.
    Set MyRange = ActiveSheet.UsedRange
'Step 3:  Start reverse looping through the range.
    For iCounter = MyRange.Columns.Count To 1 Step -1
'Step 4: If entire column is empty then delete it.
       If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
       Columns(iCounter).Delete
       End If
'Step 5: Increment the counter down
    Next iCounter
End Sub

尝试:

If Cells(Rows.Count, iCounter).End(xlUp).Row<=3 Then
  Columns(iCounter).Delete
End If

"Cells(Rows.Count, iCounter(。结束(xlUp(。行"查找列中最后一个填充单元格的行 #。 如果该行是前 3 行之一,那么我们知道它下面的所有单元格都是空的

最新更新