简体   繁体   English

VBA 删除所有带有空格的行并删除行将包含字符串的单元格

[英]VBA Delete all rows with blank space and delete rows will cell that contains string

All, I have some VBA that deletes any row in which column C is blank.所有,我有一些 VBA 删除 C 列为空白的任何行。 It also deletes and row in which Column A starts with 'Name' or 'Bar Name'.它还会删除 A 列以“名称”或“条形名称”开头的行。 The problem is when I run these all together it only runs the first 2 steps.问题是当我一起运行这些时,它只运行前两个步骤。 Furthermore, it doesn't actually turn off the filtering it's using and leaves the sheet filtered after it deletes the data.此外,它实际上并没有关闭它正在使用的过滤,而是在删除数据后过滤工作表。

Any idea how to get this to run as one step, this only seems to recognize the VBA for the first sheet?知道如何让它作为一个步骤运行,这似乎只识别第一张表的 VBA 吗?

Sub WB()
With ThisWorkbook.Worksheets("Geez")
Application.ScreenUpdating = False
Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
End With
End Sub

Sub WC()
With ThisWorkbook.Worksheets("Geez")
    .AutoFilterMode = False
    With Range("a1", Range("a" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*Bar Name*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub

Sub WE()
With ThisWorkbook.Worksheets("Class")
Application.ScreenUpdating = False
Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
End With
End Sub


Sub WF()
With ThisWorkbook.Worksheets("Class")
    .AutoFilterMode = False
    With Range("a1", Range("a" & Rows.Count).End(xlUp))
        .AutoFilter 1, "*Name*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub

I try to run all and assign to a button using the below, but it only runs this for the first two steps.我尝试使用以下命令全部运行并分配给一个按钮,但它仅在前两个步骤中运行。 It also filters the first sheet, but doesn't actually remove the filter.它还会过滤第一张纸,但实际上并没有删除过滤器。

Sub Run1()
Call WB
Call WC
Call WE
Call WF
End Sub

Delete Empty and Filter删除空并过滤

  • If you do a procedure for empty cells and another for filtering on one value, you can easily use them in another procedure.如果您为空单元格执行一个过程,另一个过程用于过滤一个值,您可以轻松地在另一个过程中使用它们。
  • Note all the occurrences of ws : they are showing where you have to put dots ( . ) instead if you plan to use the With statement : usually in front of Rows , Columns , Range , Cells ...注意ws的所有出现:如果您打算使用With statement ,它们会显示您必须放置点 ( . ) 的位置:通常在RowsColumnsRangeCells ...
  • When using On Error Resume Next (defer error handling (ignore errors)), you have to use a 'closing' On Error Goto 0 (turn off error handling).当使用On Error Resume Next (延迟错误处理(忽略错误))时,您必须使用“关闭” On Error Goto 0 (关闭错误处理)。
  • The 'slight inaccuracy' in defining the range is easily handled with Resize : you don't necessarily want to delete the row below the range.使用Resize可以轻松处理定义范围时的“轻微不准确”:您不一定要删除范围下方的行。
  • Using 12 instead of xlCellTypeVisible makes the code unnecessarily less readable.使用12而不是xlCellTypeVisible会使代码的可读性不必要地降低。

The Code编码

Option Explicit

Sub doAll()
    
    Application.ScreenUpdating = False
    
    Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Worksheets("Geez")
    doEmpty ws
    doFilter ws, "*Bar Name*"
    
    Set ws = ThisWorkbook.Worksheets("Class")
    doEmpty ws
    doFilter ws, "*Name*"
    
    Application.ScreenUpdating = True

End Sub

Sub doEmpty(ws As Worksheet)
    ws.AutoFilterMode = False
    ws.Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub

Sub doFilter(ws As Worksheet, ByVal FilterPattern As String)
    ws.AutoFilterMode = False
    With ws.Range("A1", ws.Range("A" & ws.Rows.Count).End(xlUp))
        .AutoFilter 1, FilterPattern
        On Error Resume Next
        .Resize(.Rows.Count - 1).Offset(1) _
            .SpecialCells(xlCellTypeVisible).EntireRow.Delete
        On Error GoTo 0
    End With
    ws.AutoFilterMode = False
End Sub

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

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