简体   繁体   English

根据值删除VBA中的行

[英]Delete rows in VBA dependent on value

I'm currently working on a vba project and i'm stuck on (what is essentially) the first part. 我目前正在从事vba项目,而我坚持第一部分(本质上是什么)。

I need to delete all rows from my dataset which contain the text 'False' in column I 我需要从数据集中删除第一列中包含文本“ False”的所有行

The reason for not doing this with a filter and delete rows is that i'm automating a report for others and would like the whole report generation to take place with one click of a button. 不使用过滤器和删除行来执行此操作的原因是,我正在为其他人自动化报告,并且希望整个报告生成只需单击一下按钮即可。

The code below is as far as i've got, it compiles without any errors...but doesn't seem to actually do anything. 就我所知,下面的代码可以编译,没有任何错误...但是似乎实际上没有任何作用。

Quick note, there are other sheets that contain formulas referring to this sheet, and it is part of a larger program so i can't afford to delete the sheet and only paste the 'valid' data there 快速说明,还有其他工作表包含引用此工作表的公式,并且它是较大程序的一部分,因此我无法删除工作表,仅在其中粘贴“有效”数据

Option Explicit

Sub Remfal()
  Application.ScreenUpdating = False

    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim ws1 As Worksheet

   Set ws1 = Sheets("Dataset")

   With ws1

    Firstrow = Cells(Rows.Count, "I").End(xlDown).Row
    Lastrow = Cells(Rows.Count, "I").End(xlUp).Row

    For Lrow = Lastrow To Firstrow Step -1

        With .Cells(Lrow, "I")

            If .Value = "False" Then .EntireRow.Delete

        End With
    Next Lrow
   End With
  Application.ScreenUpdating = True

End Sub

Your FirstRow is wrong, that's why. 您的FirstRow错误,这就是原因。 You start at the last row and try to go down. 您从最后一行开始,然后尝试向下移动。 It doesn't work! 没用! Try giving the first row a code name and using that instead! 尝试给第一行指定一个代号,然后改用它! Ex. 例如 If you call it FirstRow: 如果您将其称为FirstRow:

FirstRow = Sheet1.Range("FirstRow").Row

To make sure you are using the right values, write 为了确保使用正确的值,请输入

debug.print firstrow & " " & lastrow

After setting the values. 设置值之后。

I'm not sure why you want to find the first row because usually users start at the top. 我不确定为什么要查找第一行,因为通常用户从顶部开始。 Unless there are some 'false' values that you want to overlook in column I, just set the range from 1 to the last row. 除非您希望在第一列中忽略一些“假”值,否则只需将范围设置为1至最后一行。

Also, as well as setting application.screenupdating = false, I suggest you set Calculation to xlManual and put it back to xlAutomatic at the end. 另外,除了设置application.screenupdating = false之外,我建议您将Calculation设置为xlManual,最后将其放回xlAutomatic。 It will help greatly if you have formulas in your sheet. 如果您的工作表中有公式,将对您大有帮助。

I found a couple of things to clean up. 我发现有几件事需要清理。

Sub Remfal()

    Application.ScreenUpdating = False

    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim ws1 As Worksheet

    Set ws1 = Sheets(1)

    With ws1

        Firstrow = .Cells(1, "I").End(xlDown).Row 'Notice the "." in front of Cells; also i start at I1 and go down (may not be what you want), rather than start at bottom and go down
        Lastrow = .Cells(Rows.Count, "I").End(xlUp).Row
    End With

    For Lrow = Lastrow To Firstrow Step -1

        With ws1.Cells(Lrow, "I") 'You cant "With" within a "With" statement reliably in VBA; instead I closed the old statement and opened a new one

            If .Formula = False Then 'Using .Formula instead of .Value avoids picking up blank values as FALSE; also changed "False" to False, as its a boolean value not text

                .EntireRow.Delete
            End If

        End With
    Next Lrow

    Application.ScreenUpdating = True

End Sub

you don't need "With" statement to edit only one defined sheet. 您不需要“ With”语句仅编辑一个已定义的工作表。

Sub Remfal()
Application.ScreenUpdating = False

Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim ws1 As Worksheet

Set ws1 = Sheets("Dataset")
Firstrow = ws1.Range("I1").End(xlUp).Row
Lastrow = Lastrow = ws1.Cells(ws1.Rows.Count, "I").End(xlUp).Row 'edit before Grade report
For Lrow = Lastrow To Firstrow Step -1
    If ws1.Cells(Lrow, "I").Value = "False" Then ws1.Cells(Lrow, "I").EntireRow.Delete
Next Lrow
Application.ScreenUpdating = True
End Sub

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

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