【发布时间】:2018-01-28 03:38:21
【问题描述】:
下面提供了我的宏。我想删除所有行,其中甚至没有一个单元格是蓝色或红色的!因此,宏在开始时会执行一些着色,效果很好!但是,当我只想保留具有彩色单元格的行时,它无法正常工作。宏没有告诉我它有错误。它只是运行,但从未停止运行:p 有什么想法吗?非常感谢!
Sub PO()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False
Worksheets("Tracker").Cells.Copy
With Worksheets("po")
.Cells.PasteSpecial xlValues
.Cells.PasteSpecial xlFormats
End With
Sheets("po").Select
Dim mDiff1 As Double
mDiff1 = 0.01
Dim mDiff2 As Double
mDiff2 = 0.03
Dim mDiff3 As Double
mDiff3 = 0.01
Dim mDiff4 As Double
mDiff4 = 0.03
For Each cell1 In Range(Range("U2"), Range("U2").End(xlDown))
If cell1.Value - cell1.Offset(0, 1).Value > mDiff1 Then
cell1.Offset(0, 1).Interior.ColorIndex = 3
End If
If cell1.Value - cell1.Offset(0, 2).Value > mDiff2 Then
cell1.Offset(0, 2).Interior.ColorIndex = 5
End If
Next cell1
For Each cell2 In Range(Range("AB2"), Range("AB2").End(xlDown))
If cell2.Value - cell2.Offset(0, 1).Value > mDiff3 Then
cell2.Offset(0, 1).Interior.ColorIndex = 3
End If
If cell2.Value - cell2.Offset(0, 2).Value > mDiff4 Then
cell2.Offset(0, 2).Interior.ColorIndex = 5
End If
Next cell2
Dim row As Range
Dim cell3 As Range
For Each row In Range("A2", Range("A2").End(xlDown).End(xlToRight)).Rows
For Each cell3 In row.Cells
If Not cell3.Interior.ColorIndex = 3 Or cell3.Interior.ColorIndex = 5 Then
cell3.EntireRow.Delete
End If
Next cell3
Next row
Sheets("po").Select
If Not ActiveSheet.AutoFilterMode Then
ActiveSheet.Rows(1).AutoFilter
End If
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
【问题讨论】:
-
删除行时始终从下到上工作。
-
那我该怎么改呢?
-
将循环从
For Each更改为For i = lastRow to 1 Step -1。谷歌一下,你会发现很多关于如何找到最后一行的例子。然后是Cells(i,1).EntireRow.Delete -
完全一团糟:p 到处都有错误
-
复制您在上面发布的代码并尝试再次进行更改。使用 F8 单步代码。