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

时间:2018-04-19 15:30:47

标签: excel vba 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

1 个答案:

答案 0 :(得分:1)

尝试:

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

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