使用VBA根据多个条件删除或高亮显示行
我正在尝试创建一个代码来解决以下条件: 如果列C中的特定单元格等于零,则删除行 如果列U中的特定单元格以9开头,请删除行 如果列E中的特定单元格为负值,请删除行 如果C列中的特定单元格从2015开始,则突出显示颜色 如果C列中的特定单元格从2016开始,则突出显示与上述相同的颜色 如果C列中的特定单元格从2017开始,则用不同的颜色高亮显示 其他一切,离开 这就是我到目前为止所做的,我不断地遇到编码错误。 我知道这是非常具体的,非常感谢任何帮助 子模块()使用VBA根据多个条件删除或高亮显示行,vba,excel,Vba,Excel,我正在尝试创建一个代码来解决以下条件: 如果列C中的特定单元格等于零,则删除行 如果列U中的特定单元格以9开头,请删除行 如果列E中的特定单元格为负值,请删除行 如果C列中的特定单元格从2015开始,则突出显示颜色 如果C列中的特定单元格从2016开始,则突出显示与上述相同的颜色 如果C列中的特定单元格从2017开始,则用不同的颜色高亮显示 其他一切,离开 这就是我到目前为止所做的,我不断地遇到编码错误。 我知道这是非常具体的,非常感谢任何帮助 子模块() 结束Sub仅总结所有评论: Sub M
结束Sub仅总结所有评论:
Sub Module()
Dim x As Long
Dim lastrow As Long
sSheetName = ActiveSheet.Name
With Worksheets(sSheetName)
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For x = lastrow To 1 Step -1
If .Cells(x, 3).Value = 0 Then .Rows(x).Delete
If Left(.Cells(x, 21), 1) = 9 Then .Rows(x).Delete
If Left(.Cells(x, 5), 1) = "-" Then .Rows(x).Delete
If Left(.Cells(x, 3), 4) = 6017 Then
.Cells(x,1).Resize(, 21).Interior.ColorIndex = 39
ElseIf Left(.Cells(x, 3), 4) = 6018 Then
.Cells(x,1).Resize(, 21).Interior.ColorIndex = 39
ElseIf Left(.Cells(x, 3), 4) = 6150 Then
.Cells(x,1).Resize(, 21).Interior.ColorIndex = 43
Else
.Cells(x,1).EntireRow.Interior.ColorIndex = xlNone
End If
Next x
End with
End Sub
总结一下所有的评论:
Sub Module()
Dim x As Long
Dim lastrow As Long
sSheetName = ActiveSheet.Name
With Worksheets(sSheetName)
lastrow = .Cells(Rows.Count, 1).End(xlUp).Row
For x = lastrow To 1 Step -1
If .Cells(x, 3).Value = 0 Then .Rows(x).Delete
If Left(.Cells(x, 21), 1) = 9 Then .Rows(x).Delete
If Left(.Cells(x, 5), 1) = "-" Then .Rows(x).Delete
If Left(.Cells(x, 3), 4) = 6017 Then
.Cells(x,1).Resize(, 21).Interior.ColorIndex = 39
ElseIf Left(.Cells(x, 3), 4) = 6018 Then
.Cells(x,1).Resize(, 21).Interior.ColorIndex = 39
ElseIf Left(.Cells(x, 3), 4) = 6150 Then
.Cells(x,1).Resize(, 21).Interior.ColorIndex = 43
Else
.Cells(x,1).EntireRow.Interior.ColorIndex = xlNone
End If
Next x
End with
End Sub
重构代码,这应该适合您:
Sub tgr()
Dim rDelete As Range
Dim rPurple39 As Range
Dim rGreen43 As Range
Dim lLastRow As Long
Dim i As Long
With ActiveWorkbook.ActiveSheet
lLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("1:" & lLastRow).EntireRow.Interior.ColorIndex = xlNone
For i = 1 To lLastRow
If .Cells(i, "C").Value = 0 _
Or Left(.Cells(i, "U").Value, 1) = 9 _
Or Left(.Cells(i, "E").Value, 1) = "-" Then
If rDelete Is Nothing Then Set rDelete = .Rows(i) Else Set rDelete = Union(rDelete, .Rows(i))
Else
Select Case Left(.Cells(i, "C"), 4)
Case 6017, 6018: If rPurple39 Is Nothing Then Set rPurple39 = .Cells(i, "A") Else Set rPurple39 = Union(rPurple39, .Cells(i, "A"))
Case 6150: If rGreen43 Is Nothing Then Set rGreen43 = .Cells(i, "A") Else Set rGreen43 = Union(rGreen43, .Cells(i, "A"))
End Select
End If
Next i
End With
If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
If Not rPurple39 Is Nothing Then rPurple39.Interior.ColorIndex = 39
If Not rGreen43 Is Nothing Then rGreen43.Interior.ColorIndex = 43
End Sub
重构代码,这应该适合您:
Sub tgr()
Dim rDelete As Range
Dim rPurple39 As Range
Dim rGreen43 As Range
Dim lLastRow As Long
Dim i As Long
With ActiveWorkbook.ActiveSheet
lLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("1:" & lLastRow).EntireRow.Interior.ColorIndex = xlNone
For i = 1 To lLastRow
If .Cells(i, "C").Value = 0 _
Or Left(.Cells(i, "U").Value, 1) = 9 _
Or Left(.Cells(i, "E").Value, 1) = "-" Then
If rDelete Is Nothing Then Set rDelete = .Rows(i) Else Set rDelete = Union(rDelete, .Rows(i))
Else
Select Case Left(.Cells(i, "C"), 4)
Case 6017, 6018: If rPurple39 Is Nothing Then Set rPurple39 = .Cells(i, "A") Else Set rPurple39 = Union(rPurple39, .Cells(i, "A"))
Case 6150: If rGreen43 Is Nothing Then Set rGreen43 = .Cells(i, "A") Else Set rGreen43 = Union(rGreen43, .Cells(i, "A"))
End Select
End If
Next i
End With
If Not rDelete Is Nothing Then rDelete.EntireRow.Delete
If Not rPurple39 Is Nothing Then rPurple39.Interior.ColorIndex = 39
If Not rGreen43 Is Nothing Then rGreen43.Interior.ColorIndex = 43
End Sub
你会犯哪些错误?使用
.Cells
代替单元格
。cell
是变量还是输入错误?代码有什么问题?它具体在哪里分解,显示了什么错误?什么是sSheetName
?你还没有定义它。可能是字符串-在这种情况下,在分配工作表名称时删除集。此外,在的范围内使用。EntireRow.Delete
无效。您需要例如.Cells(x,3).EntireRow.Delete
编译我得到的错误:Block If without End If您得到了哪些错误?使用.Cells
代替单元格
。cell
是变量还是输入错误?代码有什么问题?它具体在哪里分解,显示了什么错误?什么是sSheetName
?你还没有定义它。可能是字符串-在这种情况下,在分配工作表名称时删除集。此外,在的范围内使用。EntireRow.Delete
无效。您需要例如.Cells(x,3).EntireRow.Delete
编译错误,我得到的错误是:如果没有结尾,则阻止。如果需要一些行连续字符(
),或者如果需要一些结束符()。@SteveLovell,但我需要去掉ElseIf
中的空格!你说得对。但是仍然需要以
结尾!对不起,为什么我们有6017、6018和6150?这应该是2015年、2016年和2017年吗?@SteveLovell IDK,我刚刚复制了OP在他们代码中的内容。这要么需要一些行连续字符(
),要么需要一些结束(如果s)。@SteveLovell但我需要去掉ElseIf
中的空格!你说得对。但是仍然需要以
结尾!对不起,为什么我们有6017、6018和6150?这应该是2015年、2016年和2017年吗?@SteveLovell IDK,我只是复制了OP在他们代码中的内容。