簡體   English   中英

刪除其列包含0的整個行,Excel 2007 VBA

[英]Deleting entire row whose column contains a 0, Excel 2007 VBA

更新:

好吧,所以我使用了下面的代碼,它執行了我需要做的事情,即檢查值是否為0,是否為0,然后刪除整行。 但是我想對一個工作簿中的多個工作表執行一次,一次。 以下代碼正在執行的操作是,它僅從當前電子表格中刪除零,當您通過VBA腳本打開excel時,默認情況下該電子表格默認為活動狀態。 這里是工作零清除代碼:

Dim wsDCCTabA As Excel.Worksheet
Dim wsTempGtoS As Excel.Worksheet

Set wsDCCTabA = wbDCC.Worksheets("Login")
Set wsTempGtoS = wbCalc.Worksheets("All_TemporaryDifferences")

Dim LastRow As Long, n As Long
LastRow = wsTempGtoS.Range("E65536").End(xlUp).Row
For n = LastRow To 1 Step -1
If Cells(n, 5).Value = 0 Then
    Cells(n, 5).EntireRow.Delete
End If
Next

我究竟做錯了什么? 當我為同一工作簿中的另一個工作表做相同的事情時,它什么也不做。 我正在使用以下代碼從工作表中刪除零:

Set wsPermGtoS = wbCalc.Worksheets("All_PermanentDifferences")
'delete rows with 0 description  
Dim LastRow As Long, n As Long
LastRow = wsPermGtoS.Range("E65536").End(xlUp).Row
For n = LastRow To 1 Step -1
If Cells(n, 5).Value = 0 Then
    Cells(n, 5).EntireRow.Delete
End If
Next

有什么想法嗎? 或做同樣事情的另一種方式?

原始問題:

我要刪除在特定列中具有零的所有行。 我正在使用以下代碼,但似乎什么也沒有發生:

 CurrRow = (Range("E65536").End(xlUp).Row)
 For Count = StartRow To CurrRow
     If wsDCCTabA.Range("E" & Count).Value = "0" Then
         wsDCCTabA.Rows(Count).Delete
     End If
 Next

StartRow包含開始的行值CurrRow包含最后使用的行的行值

看看這是否有幫助:

Sub DelSomeRows()

    Dim colNo As Long:      colNo = 5             ' hardcoded to look in col 5
    Dim ws    As Worksheet: Set ws = ActiveSheet  ' on the active sheet

    Dim rgCol As Range
    Set rgCol = ws.Columns(colNo)                          ' full col range (huge)
    Set rgCol = Application.Intersect(ws.UsedRange, rgCol) ' shrink to nec size
    Dim rgZeroCells As Range ' range to hold all the "0" cells (union of disjoint cells)
    Dim rgCell      As Range ' single cell to iterate
    For Each rgCell In rgCol.Cells
        If Not IsError(rgCell) Then
            If rgCell.Value = "0" Then
                If rgZeroCells Is Nothing Then
                    Set rgZeroCells = rgCell ' found 1st one, assign
                Else
                    Set rgZeroCells = Union(rgZeroCells, rgCell) ' found another, append
                End If
            End If
        End If
    Next rgCell
    If Not rgZeroCells Is Nothing Then
        rgZeroCells.EntireRow.Delete ' deletes all the target rows at once
    End If
End Sub

刪除行后,您需要減去“ Count”變量

CurrRow = (Range("E65536").End(xlUp).Row)
For Count = StartRow To CurrRow
    If wsDCCTabA.Range("E" & Count).Value = "0" Then
        wsDCCTabA.Rows(Count).Delete
        ' Add this line:
        Count = Count - 1
    End If
Next

我知道了。 為了將來參考,我使用了

ActiveWorkbook.Sheets("All_temporaryDifferences").Activate 

ActiveWorkbook.Sheets("All_Permanentdifferences").Activate

您不需要使用ActiveWorkbook.Sheets("All_temporaryDifferences").Activate 實際上,如果ActiveWorkbookwbCalc不同, wbCalc出現錯誤。

真正的問題是您使用的是對Cells(n, 5).Value的不合格引用。 不合格表示您未指定要使用的工作表,因此默認為活動工作表。 有時可能會起作用,但是它的代碼不正確。 在您的情況下,它不起作用。

相反,您應該始終使用合格的引用。 wsTempGtoS.Cells(n, 5).Value是合格的引用。 wsTempGtoS指定所需的工作表,這樣就不會猜測VBA。

Dim LastRow As Long, n As Long
LastRow = wsTempGtoS.Range("E65536").End(xlUp).Row
For n = LastRow To 1 Step -1
    If wsTempGtoS.Cells(n, 5).Value = 0 Then
        wsTempGtoS.Cells(n, 5).EntireRow.Delete
    End If
Next

這: CurrRow = (Range("E65536").End(xlUp).Row)也是不合格的引用。 而是應該為CurrRow = wsDCCTabA.Range("E65536").End(xlUp).Row

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM