簡體   English   中英

根據范圍中的空白單元格刪除行

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

我正在嘗試刪除范圍內的空白行

我的代碼如下所示:

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已幫助我解決了錯誤消息。 變量應調暗為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 

看來您應該嘗試將Long類型用於icounter 使用Integer至少在較新版本的Excel中會導致溢出,該工作表中有超過一百萬行。

這是一些更簡單的代碼:

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