Excel VBA: 如何读取Column数据并根据条件删除Worksheet
Excel VBA: How to read Column data and delete Worksheet based on criteria
我已经对这个主题进行了大量研究,但到目前为止还没有成功。
我有一定的编程经验。
也就是说,我的问题是关于从列中读取数据并在某些文本被读取 47 次时删除工作表。
在列“L”文本(“n/m") 重复 47 次。文本总是从第 14 行开始,一直持续到第 70 行。在该范围内有 spaces 和
"--------"
如果 那一列有 47 "n/m" 然后可以删除工作表,并且对于包含大约 40 到 100 个工作表的整个工作簿,它必须是 applied/repeated。
我的代码:
第一次尝试失败
Sub DeletingBlankPages()
Dim Ws As Worksheet
Dim nm As Range
Set nm = Ws.Range(Columns("12"))
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Worksheets
nm.Select
If nm Is "n/m" Then
Application.DisplayAlerts = False
Ws.Delete
Application.DisplayAlerts = True
End If
Next Ws
End Sub
第二次尝试还是不行
Sub DeleteRowBasedOnCriteria()
Dim Ws As Worksheet
For Each Ws In ActiveWorkbook.Worksheets
Application.DisplayAlerts = False
If Range(Columns("12")).Value < 47 > "n/m" _
Then _
Ws.Delete
Application.DisplayAlerts = True
End If
Next Ws
End Sub
如果哪位有经验的朋友知道如何解决这个问题请回复。
谢谢
试试这个:
Sub DeleteRowBasedOnCriteria()
Dim Ws As Worksheet
Dim Counter As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Worksheets
Counter = 0
For i = 14 To 70
If Ws.Cells(i, 12) = "n/m" Then
Counter = Counter + 1
End If
Next i
If Counter >= 47 Then
Ws.Delete
End If
Next Ws
Application.DisplayAlerts = True
Application.ScreenUpdating = True
结束子
如果我没理解错,试试这个
Sub DeleteRowBasedOnCriteria()
Dim i As Long
For i = Sheets.Count To 1 Step -1
If WorksheetFunction.CountIf(Sheets(i).Range("L14:L70"), "n/m") >= 47 Then
If Sheets.Count > 1 Then
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
Else
MsgBox "Only 1 sheet left"
Exit Sub
End If
End If
Next i
End Sub
我已经对这个主题进行了大量研究,但到目前为止还没有成功。 我有一定的编程经验。
也就是说,我的问题是关于从列中读取数据并在某些文本被读取 47 次时删除工作表。
在列“L”文本(“n/m") 重复 47 次。文本总是从第 14 行开始,一直持续到第 70 行。在该范围内有 spaces 和 "--------"
如果 那一列有 47 "n/m" 然后可以删除工作表,并且对于包含大约 40 到 100 个工作表的整个工作簿,它必须是 applied/repeated。
我的代码:
第一次尝试失败
Sub DeletingBlankPages()
Dim Ws As Worksheet
Dim nm As Range
Set nm = Ws.Range(Columns("12"))
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Worksheets
nm.Select
If nm Is "n/m" Then
Application.DisplayAlerts = False
Ws.Delete
Application.DisplayAlerts = True
End If
Next Ws
End Sub
第二次尝试还是不行
Sub DeleteRowBasedOnCriteria()
Dim Ws As Worksheet
For Each Ws In ActiveWorkbook.Worksheets
Application.DisplayAlerts = False
If Range(Columns("12")).Value < 47 > "n/m" _
Then _
Ws.Delete
Application.DisplayAlerts = True
End If
Next Ws
End Sub
如果哪位有经验的朋友知道如何解决这个问题请回复。 谢谢
试试这个:
Sub DeleteRowBasedOnCriteria()
Dim Ws As Worksheet
Dim Counter As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Worksheets
Counter = 0
For i = 14 To 70
If Ws.Cells(i, 12) = "n/m" Then
Counter = Counter + 1
End If
Next i
If Counter >= 47 Then
Ws.Delete
End If
Next Ws
Application.DisplayAlerts = True
Application.ScreenUpdating = True
结束子
如果我没理解错,试试这个
Sub DeleteRowBasedOnCriteria()
Dim i As Long
For i = Sheets.Count To 1 Step -1
If WorksheetFunction.CountIf(Sheets(i).Range("L14:L70"), "n/m") >= 47 Then
If Sheets.Count > 1 Then
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
Else
MsgBox "Only 1 sheet left"
Exit Sub
End If
End If
Next i
End Sub