【问题标题】:Delete columns with all empty cells and header删除所有空单元格和标题的列
【发布时间】:2018-07-25 18:26:27
【问题描述】:

下面的代码删除整个列中所有空单元格的列。我需要更改它以删除整个列中所有空单元格的列,前提是存在列标题。我希望保留没有标题且没有单元格数据的列。

感谢您的帮助!

Dim X As Long, LastRow As Long, LastCol As Long
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  LastCol = Cells(1, Columns.count).End(xlToLeft).Column
  Application.ScreenUpdating = False
  For X = LastCol To 1 Step -1
    If Cells(1, X).Value = Join(Application.Transpose(Cells(1, X).Resize(LastRow)), "") Then Columns(X).Delete
  Next
  Application.ScreenUpdating = True

【问题讨论】:

    标签: vba excel case


    【解决方案1】:

    假设第 1 行是您的标题行...

    Dim X As Long, LastRow As Long, LastCol As Long
    
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    
    Application.ScreenUpdating = False
    
    For X = LastCol To 1 Step -1
        LastRow = Cells(Rows.Count, X).End(xlUp).Row
        If LastRow = 1 And Not IsEmpty(Cells(1, X)) Then
            Columns(X).Delete
        End If
    
    Next
    
    Application.ScreenUpdating = True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      相关资源
      最近更新 更多