删除“空”行时,他们只是“出现空”

我无法设法清理“空”行的数据。 删除“0”没有问题,但那些空的单元格不是空的,而是有“空string”之类的东西。

Sub Reinigung() Application.ScreenUpdating = False Application.EnableEvents = False ListeEnde3 = ThisWorkbook.Sheets("input").Cells(Rows.Count, 1).End(xlUp).Row For Zeile1 = 2 To ListeEnde3 If ThisWorkbook.Sheets("input").Cells(Zeile1, 14) = "0" Or ThisWorkbook.Sheets("2018").Cells(Zeile1, 14) = "" Then ThisWorkbook.Sheets("input").Rows(Zeile1).Delete Zeile1 = Zeile1 - 1 Else End If Next ' ThisWorkbook.Sheets("import").Columns(14).SpecialCells(xlCellTypeBlanks).EntireRow.Delete Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

该代码只是冻结我的Excel,如果我省略了thisWorkbook.Sheets("2018").Cells(Zeile1, 14) = ""部分,它的工作原理和删除所有行,其中列14包含一个“0”。

如果我检查显示为空白的单元格= isblank,则返回“false”。 单元格中没有“空格”,也没有“”“。

该怎么办?

编辑

第一次提示后我的代码看起来像这样:

 Sub Reinigung() Dim ListeEnde3 As Long Dim Zeile1 As Long Application.ScreenUpdating = False Application.EnableEvents = False ListeEnde3 = ThisWorkbook.Sheets("import").Cells(Rows.Count, 1).End(xlUp).Row For Zeile1 = ListeEnde3 To 2 Step -1 Set rngX = ThisWorkbook.Sheets("import").Cells(Zeile1, 14) If (rngX = "0" Or rngX = "") Then 'or rngY = vbNullString ThisWorkbook.Sheets("import").Rows(Zeile1).Delete End If Next Zeile1 ' ThisWorkbook.Sheets("import").Columns(14).SpecialCells(xlCellTypeBlanks).EntireRow.Delete Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

Excel仍然崩溃/冻结(我等了5分钟),但由于代码运行与F8“顺利”我想给它一个数据较less的一个镜头:它的工作!

如果我不减less的数据有〜70000行检查。 我让它运行在720行,它的工作。

任何方式来调整代码的方式,它可以处理70000 +行? 我不认为这会太多。

谢谢!

另一种方法是简单地使用内部数组并写出具有有效行的新数据集。

这是非常快的。

如果你的数据集有公式,那么你将不得不使用额外的代码,但如果它只是常数,那么下面的代码应该是这样的:

 Sub Reinigung() 'Here I test with column E to Z, set Ranges appropriately Application.ScreenUpdating = False Application.EnableEvents = False Dim ListeEnde3 As Long, x As Long, y As Long 'last row of data - set to column of non-blank data ListeEnde3 = ThisWorkbook.Sheets("import").Cells(Rows.Count, 5).End(xlUp).Row Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("import") Dim startCell As Range 'set to whatever cell is the upper left corner of data Set startCell = ThisWorkbook.Sheets("import").Range("E1") Dim arr As Variant, arrToPrint() As Variant 'Get rightmost column of data instead of hardcoding to "Z" 'write dataset into an array arr = ws.Range(startCell, ws.Range("Z" & ListeEnde3)).Value x = UBound(arr) - LBound(arr) + 1 'num of rows of data y = UBound(arr, 2) - LBound(arr, 2) + 1 'num of columns of data ReDim arrToPrint(1 To x, 1 To y) 'array to hold valid/undeleted data Dim i As Long, j As Long, printCounter As Long, arrayColumnToCheck as Long arrayColumnToCheck = 14 - startCell.Column + 1 '14 is column N For i = 1 To x If arr(i, arrayColumnToCheck ) <> 0 And arr(i, arrayColumnToCheck ) <> vbNullString Then printCounter = printCounter + 1 For j = 1 To y 'put rows to keep in arrToPrint arrToPrint(printCounter, j) = arr(i, j) Next j End If Next i 'Print valid rows to keep - only values will print - no formulas startCell.Resize(printCounter, y).Value = arrToPrint 'Delete the rows with zero & empty cells off the sheet startCell.Offset(printCounter).Resize(ListeEnde3 - printCounter, y).Delete xlShiftUp Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

您可以使用AutoFilter并删除可见行(未testing):

 Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("import") ws.UsedRange.AutoFilter 14, Array("=0", "="), xlFilterValues ws.UsedRange.Offset(1).EntireRow.Delete ws.AutoFilterMode = False 

您可以将IsEmpty添加到您的代码来检查单元格填充

 Sub Reinigung() Application.ScreenUpdating = False Application.EnableEvents = False ListeEnde3 = ThisWorkbook.Sheets("input").Cells(Rows.Count, 1).End(xlUp).Row For Zeile1 = 2 To ListeEnde3 Set rngX = ThisWorkbook.Sheets("input").Cells(Zeile1, 14) Set rngY = ThisWorkbook.Sheets("2018").Cells(Zeile1, 14) If (rngX = "0" And (Not IsEmpty(rngX))) Or (rngY = "") Then ThisWorkbook.Sheets("input").Rows(Zeile1).Delete Zeile1 = Zeile1 - 1 End If Next ' ThisWorkbook.Sheets("import").Columns(14).SpecialCells(xlCellTypeBlanks).EntireRow.Delete Application.ScreenUpdating = True Application.EnableEvents = True End Sub 

永远不要改变一个循环计数器: Zeile1 = Zeile1 - 1

而是从最后开始,在循环中使用步骤-1来反向工作。 你处于一个无限循环,因为循环不能前进。 如果Zeile = 3并且在'2018'表的第3行中有一个“”,那么它将始终卡在Zeile1 = 3行上。 你将会一直回到'2018'表格第3行的那个“”。

 For Zeile1 = ListeEnde3 To 2 Step -1 Set rngX = ThisWorkbook.Sheets("input").Cells(Zeile1, 14) Set rngY = ThisWorkbook.Sheets("2018").Cells(Zeile1, 14) If (rngX = "0" Or rngY = "") Then 'or rngY = vbNullString ThisWorkbook.Sheets("input").Rows(Zeile1).Delete End If Next Zeile1