删除基于范围内的空白单元格的行

我想删除范围内的空白行

我的代码如下所示:

Dim rng As Range Dim i As Long, counter As Long i = 1 Range("B1").Select Selection.End(xlDown).Offset(0, 5).Select Set rng = Range("G2", ActiveCell) Range("G2").Select For counter = 1 To rng.Rows.Count If rng.Cells(i) = "" Then rng.Cells(i).EntireRow.Delete Else i = i + 1 End If Next 

所以,hmqcnoesy好心的帮我解决了错误信息。 variables应该变暗LONG not INTEGER,因为整数不能容纳我的所有数据行的大数字

而且,Jon49给了我一些代码,这个代码在这个过程中效率很高:

 Dim r1 As Range 'Using Tim's range. Set r1 = ActiveSheet.Range(Range("G2"),Range("B1").End(xlDown).Offset(0, 5)) 'Delete blank cell rows. r1.SpecialCells(xlCellTypeBlanks).EntireRow.Delete Set r1 = Nothing 

它看起来像你应该尝试使用typesLong icounter 。 使用Integer会导致溢出,至less在新版本的Excel中,工作表中有超过100万行。

这里有一些比较简单的代码:

 Dim r1 As Range 'Using Tim's range. Set r1 = ActiveSheet.Range(Range("G2"),Range("B1").End(xlDown).Offset(0, 5)) 'Delete blank cell rows. r1.SpecialCells(xlCellTypeBlanks).EntireRow.Delete Set r1 = Nothing 
 Dim rng As Range Dim counter As Long Set rng = Range(Range("G2"),Range("B1").End(xlDown).Offset(0, 5)) For counter = rng.Rows.Count to 1 Step -1 If Len(rng.Cells(counter).Value) = 0 Then rng.Cells(counter).EntireRow.Delete Next