Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 我如何更新代码以删除空格,同时删除特定的;字符串“;?_Excel_Vba - Fatal编程技术网

Excel 我如何更新代码以删除空格,同时删除特定的;字符串“;?

Excel 我如何更新代码以删除空格,同时删除特定的;字符串“;?,excel,vba,Excel,Vba,如果列A中的单元格为空,如何更新代码以删除该行;如果列A中的单元格包含字符串“性别”,如何更新代码以删除该行 我假设我需要更新:列(“A:A”).SpecialCells(xlcelltypebanks).EntireRow.Delete Application.ScreenUpdating = False For Each ws In Worksheets 'and here Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow

如果列A中的单元格为空,如何更新代码以删除该行;如果列A中的单元格包含字符串“性别”,如何更新代码以删除该行

我假设我需要更新:
列(“A:A”).SpecialCells(xlcelltypebanks).EntireRow.Delete

Application.ScreenUpdating = False
For Each ws In Worksheets 'and here
    Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    For Each MyCell In ws.Range("A2:EA2")
        If Not IsInArray(MyCell, arr) Then
            If HideMe Is Nothing Then
                Set HideMe = MyCell
            Else
                Set HideMe = Union(HideMe, MyCell)
            End If
        End If
    Next MyCell

    If Not HideMe Is Nothing Then
        HideMe.EntireColumn.Hidden = True
    End If

    Set HideMe = Nothing 'and here
Next ws 'and here
Application.ScreenUpdating = True    
这相当简单:

  • 循环浏览每个工作表
  • 确定当前工作表中的最后一行(
    lr
  • 循环a列中的一系列单元格
  • 如果满足条件,则删除该行
  • 注意,这不是代码的执行顺序,而是代码的自上而下的解释

    如果你有任何问题,请告诉我


    另外,请避免将整个代码粘贴到 问题。您发布的代码应该只包含与问题相关的信息,如下所示:

    非常简单:

  • 循环浏览每个工作表
  • 确定当前工作表中的最后一行(
    lr
  • 循环a列中的一系列单元格
  • 如果满足条件,则删除该行
  • 注意,这不是代码的执行顺序,而是代码的自上而下的解释

    如果你有任何问题,请告诉我


    另外,请避免将整个代码粘贴到
    问题。您发布的代码应仅包含与问题相关的信息,如下所示:

    处理此循环的选项:

    1)。第一个选项是在满足条件时逐行删除:

    Option Explicit
    Private Sub remove_blank_or_gender()
    
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    StartTime = Timer
    
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        Dim lr As Long 'last row
        lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
        Dim i As Long
        For i = lr To 1 Step -1
            If IsEmpty(ws.Cells(i, 1)) Or ws.Cells(i, 1) = "Gender" Then
                ws.Rows(i).EntireRow.Delete
            End If
        Next i
    Next ws
    
    SecondsElapsed = Round(Timer - StartTime, 2)
    Debug.Print SecondsElapsed
    
    End Sub
    
    在一个工作簿上执行此代码,其中一个工作表的a列第1-1000行全部填充了“性别”值,将导致运行时:

    2)。使用
    Union
    功能的选项二:

    Option Explicit
    Private Sub remove_blank_or_gender()
    
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    Dim RNG As Range
    StartTime = Timer
    
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        Set RNG = Nothing
        Dim lr As Long 'last row
        lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
        Dim i As Long
        For i = lr To 1 Step -1
            If IsEmpty(ws.Cells(i, 1)) Or ws.Cells(i, 1) = "Gender" Then
                If Not RNG Is Nothing Then
                    Set RNG = Union(RNG, Range(ws.Cells(i, 1).Address))
                Else
                    Set RNG = Range(ws.Cells(i, 1).Address)
                End If
            End If
        Next i
    RNG.Rows.EntireRow.Delete
    Next ws
    
    SecondsElapsed = Round(Timer - StartTime, 2)
    Debug.Print SecondsElapsed
    
    End Sub
    
    这将导致运行时:

    代码有点凌乱,但我的意图是提供两个选项来显示运行时的差异:)。请注意,这些测试是在本地完成的,时间可能会有所不同


    祝你好运

    处理此循环的选项如下:

    1)。第一个选项是在满足条件时逐行删除:

    Option Explicit
    Private Sub remove_blank_or_gender()
    
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    StartTime = Timer
    
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        Dim lr As Long 'last row
        lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
        Dim i As Long
        For i = lr To 1 Step -1
            If IsEmpty(ws.Cells(i, 1)) Or ws.Cells(i, 1) = "Gender" Then
                ws.Rows(i).EntireRow.Delete
            End If
        Next i
    Next ws
    
    SecondsElapsed = Round(Timer - StartTime, 2)
    Debug.Print SecondsElapsed
    
    End Sub
    
    在一个工作簿上执行此代码,其中一个工作表的a列第1-1000行全部填充了“性别”值,将导致运行时:

    2)。使用
    Union
    功能的选项二:

    Option Explicit
    Private Sub remove_blank_or_gender()
    
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    Dim RNG As Range
    StartTime = Timer
    
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Sheets
        Set RNG = Nothing
        Dim lr As Long 'last row
        lr = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
        Dim i As Long
        For i = lr To 1 Step -1
            If IsEmpty(ws.Cells(i, 1)) Or ws.Cells(i, 1) = "Gender" Then
                If Not RNG Is Nothing Then
                    Set RNG = Union(RNG, Range(ws.Cells(i, 1).Address))
                Else
                    Set RNG = Range(ws.Cells(i, 1).Address)
                End If
            End If
        Next i
    RNG.Rows.EntireRow.Delete
    Next ws
    
    SecondsElapsed = Round(Timer - StartTime, 2)
    Debug.Print SecondsElapsed
    
    End Sub
    
    这将导致运行时:

    代码有点凌乱,但我的意图是提供两个选项来显示运行时的差异:)。请注意,这些测试是在本地完成的,时间可能会有所不同


    祝你好运

    删除空格,然后循环检查性别或使用自动筛选或查找。删除空格,然后循环检查性别或使用自动筛选或查找。感谢您的代码。我希望它能贯穿所有的工作表。我想我可以将工作表名称作为参数传入?@Alex您可以,但是对于每个循环,使用一个简单的
    更容易,请检查我编辑的答案(
    ,用于此工作簿中的每个ws.Sheets
    )@Rawrplus,例如,如果下面两行都有“性别”,这将不正确。在第一个
    .Delete
    之后,它将跳过最初位于已删除行下方的行。而是使用统一的范围并立即删除;)<对于每个
    ,删除行是个坏主意-可能会跳过某些行
    For i=lr到1步骤-1
    是可能的方法之一。@JvdV是的,爱上了书中最古老的把戏(再一次,叹息..)。不知怎的,我没有想到这会发生。使用trusty
    lr to 1
    算法相应地编辑代码。现在应该没事了谢谢你的密码。我希望它能贯穿所有的工作表。我想我可以将工作表名称作为参数传入?@Alex您可以,但是对于每个循环,使用一个简单的
    更容易,请检查我编辑的答案(
    ,用于此工作簿中的每个ws.Sheets
    )@Rawrplus,例如,如果下面两行都有“性别”,这将不正确。在第一个
    .Delete
    之后,它将跳过最初位于已删除行下方的行。而是使用统一的范围并立即删除;)<对于每个
    ,删除行是个坏主意-可能会跳过某些行
    For i=lr到1步骤-1
    是可能的方法之一。@JvdV是的,爱上了书中最古老的把戏(再一次,叹息..)。不知怎的,我没有想到这会发生。使用trusty
    lr to 1
    算法相应地编辑代码。现在应该没事了