[英]Deleting specific rows according to the cell value and column heading
Below is the code I wrote, which deletes the rows that contain the value "PRODUCTION"
in column M
下面是我编写的代码,该代码删除了
M
列中包含"PRODUCTION"
值的行
Sub DeleteProducts()
Dim LR, i As Long
Dim RNG As Range
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
LR = ws.Cells(Rows.Count, "M").End(xlUp).Row
For i = LR To 2 Step -1
Select
Case ws.Cells(i, "M").Value
Case Is <> "Production"
ws.Cells(i, "M").EntireRow.Delete shift:=xlUp
End Select
Next i
Next ws
End Sub
I need rows to be deleted in multiple sheets according to the column header because column name may change ( M
to something else) but the header will be the same in every sheet. 我需要根据列标题在多张工作表中删除行,因为列名可能会更改(
M
更改为其他名称),但是每张工作表中的标题都相同。
I assume that the header of the column is in the first row of each worksheet: 我假设该列的标题在每个工作表的第一行中:
Sub DeleteProducts()
Dim LR as Long, LC as Long, i As Long, j As Long
Dim RNG As Range
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
LC = ws.Cells(1, Columns.Count).End(xlToRight).Column
For i = LC To 2 Step -1
If ws.Cells(1, i) = "YOURnameHERE" Then
LR = ws.Cells(Rows.Count, i).End(xlUp).Row
Exit For
End If
Next
For j = LR To 2 Step -1
If ws.Cells(j, i).Value <> "Production" Then ws.Cells(j, i).EntireRow.Delete shift:=xlUp
Next
Next ws
End Sub
This will find the name of the column, and then store in i
that column's number. 这将找到的列的名称,然后将其保存在
i
那一列的号码。 With that information you can then find the last row of that very column, and look for every value that is not = "Production". 利用这些信息,您可以找到该列的最后一行,并查找每个不等于“ Production”的值。
I also corrected some other bits of code, just for it to be cleaner. 我还纠正了其他一些代码,只是为了使其更整洁。
Please use Range.Find to find the target column. 请使用Range.Find查找目标列。 Modified your code below.
在下面修改了您的代码。
Sub DeleteProducts()
Dim LR, i As Long
Dim RNG As Range
Dim ws As Worksheet
Dim rngTargetColumn as range
For Each ws In ActiveWorkbook.Sheets
Set rngTargetColumn = ws.Range("1:1").Find("*<Column Heading>*") 'Replace <Column Heading> with your column header string
if not rngTargetColumn is Nothing then
LR = ws.Cells(Rows.Count, rngTargetColumn.Column).End(xlUp).Row
For i = LR To 2 Step -1
If ws.Cells(i, rngTargetColumn.Column).Value <> "Production" Then
ws.Cells(i, rngTargetColumn.Column).EntireRow.Delete shift:=xlUp
End If
Next i
Set rngTargetColumn = Nothing
End If
Next ws
End Sub
Her is my shot at the task. 她是我的任务。 The code searches for the desired header in the first row on all sheets.
该代码在所有工作表的第一行中搜索所需的标题。 If the header is found, the search for "Production" continues in the column in witch the header was found.
如果找到标题,则在找到标题的栏中继续搜索“生产”。
EDIT: Did some minor cleanup of the code. 编辑:做了一些小的清理代码。
Sub DeleteRowProduction()
Dim Header As Range
Dim FoundCell As Range
Dim ws As Worksheet
Dim HeaderToFind As String
Dim ValueToFind As String
HeaderToFind = "aaa"
ValueToFind = "Production"
For Each ws In Worksheets
Set Header = ws.Rows(1).Find(what:=HeaderToFind, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
If Not Header Is Nothing Then
Set FoundCell = ws.Columns(Header.Column).Find(what:=ValueToFind, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
Do While Not FoundCell Is Nothing
ws.Rows(FoundCell.Row).Delete
Set FoundCell = Nothing
Set FoundCell = ws.Columns(Header.Column).Find(what:=ValueToFind, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
Loop
End If
Next ws
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.