2017-03-16 90 views
1

我在計算如何刪除整行(如果該行中的單元格等於)時遇到問題。根據單元值刪除整行

這是我寫的,關於如何使它工作的任何想法?

Dim rng As Range, rrng As Range 
    Set rng = Range("A1:E1") 
    Set rng = Range(rng, rng.End(xlDown)) 
    For Each rrng In rng 
    If rrng.Value = 0 Then .EntireRow.Delete 
Next rrng 
+2

這是您的上一個問題的後續行動,您將所有空白轉換爲零? – user3598756

回答

1

我做了一些細微的修改你的代碼,我認爲現在是你想要它做的事情。

Dim rng As Range, rrng As Range 
Set rng = Sheet1.Range("A1:E1") 
Set rng = Range(rng, rng.End(xlDown)) 
For Each rrng In rng 
If rrng.Value = 0 Then Sheet1.Rows(rrng.Row).Delete 
Next rrng 
0

要刪除空行,請使用反向循環...

Option Explicit 
Public Sub Example() 
    Dim Row&, i& 

    i = Cells.SpecialCells(xlCellTypeLastCell).Row 

    For Row = i To 1 Step -1 
     Debug.Print Row ' Immediate Window 
     If Application.CountA(Rows(Row)) = 0 Then 
      i = i - 1 

      If IsEmpty(Cells(Row, 1)) Then 
       Rows(Row).Delete 
      End If 
     End If 
    Next 
End Sub 
1

這是我的解決方案。它總結了所有值行並刪除它時,和是0

Sub deleting_empty_rows() 

Dim rng, rrng, rcell As Range 
Dim i, j As Long 

Set rng = Range("A1:E1") 
Set rng = Range(rng, rng.End(xlDown)) 

For j = rng.Rows.Count To 1 Step -1 

    Set rrng = rng.Rows(j) 

    i = 0 

    For Each rcell In rrng.Cells 
     i = i + rcell.Value 
    Next rcell 

    If i = 0 Then rrng.EntireRow.Delete 

Next j 

End Sub