从数据中查找和删除零

所以我在新手水平与VBA工作,我试图使用此代码find零值,并从我的数据集中删除它们。

Sub delete_empty_cells() Dim lRow As Long Dim iCol As Long Dim iCntr As Long lRow = 73 iCol = 50 For iCol = 50 To 1 Step -1 For iCntr = lRow To 1 Step -1 If Cells(iCntr, 1) = 0 Then Range(iCol & iCntr).Delete Shift:=xlUp End If Next Next End Sub 

我的数据集是73乘50,但大小可以改变,所以解释如何改变parsing的数据的大小将是恒星。 我其实对这里发生的事情有很小的想法,所以任何帮助将不胜感激。

下面的子代先find并用空白replace所有的零(与全细胞匹配),然后用SpecialCells方法删除所有的空白单元格;

 Option Explicit Sub FindAndDeleteZeros() Dim sht As Worksheet Dim fnd As Variant Dim rplc As Variant Dim intCol As Integer Dim intRow As Integer fnd = "0" rplc = "" Set sht = ActiveSheet sht.Cells.Replace what:=fnd, Replacement:=rplc, _ LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False intRow = 73 For intCol = 1 To 50 '1st to 50th column sht.Range(sht.Cells(1, intCol), sht.Cells(intRow, intCol)). _ SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp Next intCol End Sub 

查看注释中提供的链接,以避免对模块中的最后一列和最后一行进行硬编码。