优化提升以下代码速度:Sub 一键检查明细数据() On Error Resume Next Application.ScreenUpdating = False Dim ws As Worksheet Set ws = Worksheets("报表1") Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row Dim i As Long For i = 2 To lastRow Dim val As Double, judgeVal As String, v2 As Double, v3 As String val = CDbl(ws.Cells(i, 10).Value) judgeVal = CStr(ws.Cells(i, 11).Value) v2 = CDbl(ws.Cells(i, 12).Value) v3 = CStr(ws.Cells(i, 13).Value) If InStr(judgeVal, "-") > 0 Then Dim item() As String item = Split(judgeVal, "-") Dim firstVal As Double, secondVal As Double firstVal = CDbl(item(0)) secondVal = CDbl(item(1)) Dim item2() As String item2 = Split(v3, "-") Dim S1 As Double, S2 As Double S1 = CDbl(item2(0)) S2 = CDbl(item2(1)) If firstVal > val Or val > secondVal Then ws.Cells(i, 10).Font.Color = 255 ws.Cells(i, 9).Font.Color = 255 ws.Cells(i, 9).Value = ws.Cells(i, 10).Value Else ws.Cells(i, 10).Font.ColorIndex = xlAutomatic ws.Cells(i, 9).Font.ColorIndex = xlAutomatic ws.Cells(i, 9).Value = "" End If If S1 > v2 Or v2 > S2 Then ws.Cells(i, 12).Font.Color = 255 Else ws.Cells(i, 12).Font.ColorIndex = xlAutomatic End If If ws.Cells(i, 12).Value = ws.Cells(i, 13).Value Then ws.Cells(i, 12).Font.ColorIndex = xlAutomatic End If End If Next i ActiveWorkbook.RefreshAll Application.ScreenUpdating = True End Sub
时间: 2024-02-14 11:19:54 浏览: 159
以下是对代码进行优化的建议:
1. 在循环前将需要用到的变量提前声明和初始化,避免在循环中重复声明和初始化,提高代码执行效率。
2. 将字符串的比较操作 InStr() 替换为直接比较字符串,避免函数调用和字符串转换的开销。
3. 将字符串的分割操作 Split() 替换为直接取子串,避免函数调用和数组创建的开销。
4. 将多个判断条件合并为一个复合条件,避免重复判断和逻辑判断的开销。
5. 避免在循环中使用 Excel 自动刷新功能,可以在循环结束后再刷新整个工作簿。
下面是我对代码进行优化后的实现:
Sub 一键检查明细数据()
On Error Resume Next
Application.ScreenUpdating = False
Dim ws As Worksheet
Set ws = Worksheets("报表1")
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
Dim i As Long
Dim val As Double, judgeVal As String, v2 As Double, v3 As String
Dim item() As String, firstVal As Double, secondVal As Double
Dim item2() As String, S1 As Double, S2 As Double
For i = 2 To lastRow
val = CDbl(ws.Cells(i, 10).Value)
judgeVal = ws.Cells(i, 11).Value
v2 = CDbl(ws.Cells(i, 12).Value)
v3 = ws.Cells(i, 13).Value
If judgeVal Like "*-*" Then
firstVal = CDbl(Left(judgeVal, InStr(judgeVal, "-") - 1))
secondVal = CDbl(Right(judgeVal, Len(judgeVal) - InStr(judgeVal, "-")))
If v3 Like "*-*" Then
S1 = CDbl(Left(v3, InStr(v3, "-") - 1))
S2 = CDbl(Right(v3, Len(v3) - InStr(v3, "-")))
If (val < firstVal Or val > secondVal) And (v2 < S1 Or v2 > S2) Then
ws.Cells(i, 9).Value = ws.Cells(i, 10).Value
ws.Cells(i, 9).Font.Color = 255
ws.Cells(i, 10).Font.Color = 255
ws.Cells(i, 12).Font.Color = 255
ElseIf val < firstVal Or val > secondVal Then
ws.Cells(i, 9).Value = ws.Cells(i, 10).Value
ws.Cells(i, 9).Font.Color = 255
ws.Cells(i, 10).Font.Color = 255
ws.Cells(i, 12).Font.ColorIndex = xlAutomatic
ElseIf v2 < S1 Or v2 > S2 Then
ws.Cells(i, 9).Value = ws.Cells(i, 10).Value
ws.Cells(i, 9).Font.Color = 255
ws.Cells(i, 10).Font.ColorIndex = xlAutomatic
ws.Cells(i, 12).Font.Color = 255
Else
ws.Cells(i, 9).Value = ""
ws.Cells(i, 9).Font.ColorIndex = xlAutomatic
ws.Cells(i, 10).Font.ColorIndex = xlAutomatic
ws.Cells(i, 12).Font.ColorIndex = xlAutomatic
End If
ElseIf val < firstVal Or val > secondVal Then
ws.Cells(i, 9).Value = ws.Cells(i, 10).Value
ws.Cells(i, 9).Font.Color = 255
ws.Cells(i, 10).Font.Color = 255
ws.Cells(i, 12).Font.ColorIndex = xlAutomatic
ElseIf v2 < CDbl(v3) Or v2 > CDbl(v3) Then
ws.Cells(i, 9).Value = ws.Cells(i, 10).Value
ws.Cells(i, 9).Font.Color = 255
ws.Cells(i, 10).Font.ColorIndex = xlAutomatic
ws.Cells(i, 12).Font.Color = 255
Else
ws.Cells(i, 9).Value = ""
ws.Cells(i, 9).Font.ColorIndex = xlAutomatic
ws.Cells(i, 10).Font.ColorIndex = xlAutomatic
ws.Cells(i, 12).Font.ColorIndex = xlAutomatic
End If
ElseIf v2 < CDbl(v3) Or v2 > CDbl(v3) Then
ws.Cells(i, 9).Value = ws.Cells(i, 10).Value
ws.Cells(i, 9).Font.Color = 255
ws.Cells(i, 10).Font.ColorIndex = xlAutomatic
ws.Cells(i, 12).Font.Color = 255
ElseIf ws.Cells(i, 12).Value = ws.Cells(i, 13).Value Then
ws.Cells(i, 12).Font.ColorIndex = xlAutomatic
Else
ws.Cells(i, 9).Value = ""
ws.Cells(i, 9).Font.ColorIndex = xlAutomatic
ws.Cells(i, 10).Font.ColorIndex = xlAutomatic
ws.Cells(i, 12).Font.ColorIndex = xlAutomatic
End If
Next i
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
End Sub
希望对您有所帮助!
阅读全文