如果A列中的单元格为空白,如何更新代码以删除该行,如果A列中的单元格包含字符串“ Gender”,如何删除该行?
我认为我需要更新:Columns("A:A").SpecialCells(xlCellTypeBlanks).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
答案 0 :(得分:2)
这很简单:
lr
)请注意,此^不是代码执行的顺序,而是代码的从上到下的解释
Option Explicit
Private Sub remove_blank_or_gender()
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
End Sub
如果您有任何疑问,请告诉我。
此外,请避免将整个代码粘贴到您的 题。您发布的代码应仅包含与问题相关的信息,如下:Minimal, Complete and Verifiable Example
答案 1 :(得分:2)
这里提供了处理此循环的选项:
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
这导致运行时为:
代码有点混乱,但是我的意图是提供两个选项来显示运行时的不同:)。请注意,这些测试是在本地进行的,时间可能会有所不同!
祝你好运!