【问题标题】:Delete Excel Column If 1000> Cells Are Empty如果 1000> 单元格为空,则删除 Excel 列
【发布时间】:2018-02-11 18:22:42
【问题描述】:

我刚刚使用 Autodesk 文件中的数据通过算法填充了一个表。很多字段是空的,事实上,许多列是空的,但只有几行。大约有 1300 列,我正在尝试减少它。

如果列为空的行超过 1000 行,谁能帮助我使用 VBA 删除列?我真的不知道任何 VBA,所以我不知道从哪里开始......

我正在尝试这样的事情:

Sub ClearColumns()
'
' ClearColumns Macro
' Check to see if more than 1000 rows of a column are empty.  If so, delete the column.

Dim iColumn As Long
Dim iRow As Long
Dim iColumnMax As Long
Dim iRowMax As Long
Dim iEmptyCount
iColumnMax = 13000
iRowMax = 20000

For iColumn = 1 To iColumnMax
    For iRow = 1 To iRowMax
        If (Cells(iRow, iColumn) = "") Then iEmptyCount.Add (1)
        End If
    If (iEmptyCount > 999) Then Columns(iColumn).Delete
    Step 1
    End If
Step 1     
Application.Goto Reference:="ClearColumns"
End Sub

但同样,我不知道自己在做什么。任何帮助/反馈表示赞赏

【问题讨论】:

  • 但是每一列都有数百万个单元格............底部的大部分单元格都是空的!
  • 好的。然后有一系列行......就像 RowMax = 20000

标签: vba excel multiple-columns


【解决方案1】:
Sub DeleteColumns()

Dim ColNumber as Long

For ColNumber = 1300 to 1 Step -1
    If WorksheetFunction.CountBlank(Cells(1, ColNumber).Resize(20000,1)) > 1000 Then
        Cells(1,ColNumber).EntireColumn.Delete
    End If
Next

End Sub

【讨论】:

    【解决方案2】:

    这是一个删除完全空列的代码。 最后一列分配在第一行,即第一行的stub cell必须用任意符号填充。

    Sub DeleteColumns()
    Dim i As Long, lc As Long, lr As Long
    
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    For i = 1 To lc
    
    lr = Cells(Rows.Count, i).End(xlUp).Row
    If IsEmpty(Cells(lr, i)) = True Then
    Cells(lr, i).EntireColumn.Delete
    i = i - 1
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    If lc = i Then Exit For
    End If
    Next i
    
    End Sub
    

    【讨论】:

    • 我永远不会有一个完全空的列 - 只是一个几乎空的列。这就是为什么我需要检查每列是否有超过 1000 行为空
    • 从第一行开始应该是空的,还是列中间可以有1000行?
    【解决方案3】:

    这归功于 2015 年的解决方案 @Jeeped。它将删除您使用范围内的所有空单元格。

     With Worksheets("name of your worksheet")
         .UsedRange.Cells.SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
     End With
    

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 2019-06-18
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 2022-07-05
      相关资源
      最近更新 更多