简体   繁体   English

根据范围中的空白单元格删除行

[英]Delete rows based off blank cells in a range

I am trying to delete blank rows in a range 我正在尝试删除范围内的空白行

My code looks like this : 我的代码如下所示:

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

So, hmqcnoesy has kindly helped me solve the error message. 因此,hmqcnoesy已帮助我解决了错误消息。 The variables should be Dimmed as LONG not INTEGER because integer can not hold as big a number for all my rows of data 变量应调暗为LONG NOT INTEGER,因为整数不能容纳我所有数据行中那么大的数字

Also, Jon49 gave me some code that was much mroe efficient for this process: 此外,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 

It looks like you should try using type Long for i and counter . 看来您应该尝试将Long类型用于icounter Using Integer causes an overflow, at least in newer versions of Excel, where there are over 1 million rows in a worksheet. 使用Integer至少在较新版本的Excel中会导致溢出,该工作表中有超过一百万行。

Here's some simpler code for you: 这是一些更简单的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM