[英]Is there a more efficient method for For-Next including If-Then statement?
I am in a process of improving the efficiency of two macros. 我正在提高两个宏的效率。 I have managed to improve all of their methods besides the one described on the title. 除了标题中描述的方法之外,我已经设法改善了它们的所有方法。 It is fully functional, but I am sure there is a better way to right the part of the code I am providing below: 它具有完整的功能,但是我敢肯定,有一种更好的方法可以纠正下面提供的部分代码:
For Each cell2 In Range("L2:L" & lastrow2)
If Not cell2.Offset(0, -1).Value = 0 Then
If cell2.Offset(0, -5).Value = "SOCHACZEW" Then
cell2.Value = 31.2
ElseIf cell2.Offset(0, -5).Value = "SEKERPINAR" Then
cell2.Value = 33
ElseIf cell2.Offset(0, -5).Value = "ATHENS" Then
cell2.Value = 28
ElseIf cell2.Offset(0, -5).Value = "MECHELEN" Then
cell2.Value = 33
ElseIf cell2.Offset(0, -5).Value = "TIMISOARA" Then
cell2.Value = 34
ElseIf cell2.Offset(0, -5).Value = "STRANCICE" Then
cell2.Value = 33
ElseIf cell2.Offset(0, -5).Value = "KLIPPAN" Then
cell2.Value = 33
ElseIf cell2.Offset(0, -5).Value = "MATARO" Then
cell2.Value = 33
ElseIf cell2.Offset(0, -5).Value = "KIEV" Then
cell2.Value = 32
ElseIf cell2.Offset(0, -5).Value = "ROSTOV" Then
cell2.Value = 32.6
ElseIf cell2.Offset(0, -5).Value = "ITELLA" Then
cell2.Value = 32
End If
End If
Next cell2
You can combine a few of your ElseIf
s together, as they share the same result, together with switching to Select Case
, your code could be shorter as follows: 您可以将几个ElseIf
合并在一起,因为它们共享相同的结果,并且切换到Select Case
,您的代码可以缩短,如下所示:
For Each cell2 In Range("L2:L" & lastrow2)
With cell2
If Not .Offset(0, -1).Value = 0 Then
Select Case .Offset(0, -5).Value
Case "SOCHACZEW"
.Value = 31.2
Case "SEKERPINAR", "MECHELEN", "STRANCICE", "KLIPPAN", "MATARO"
.Value = 33
Case "ATHENS"
.Value = 28
Case "KIEV", "ITELLA"
.Value = 32
Case "ROSTOV"
.Value = 32.6
Case "TIMISOARA"
.Value = 34
End Select
End If
End With
Next cell2
@ShaiRado answer does make the code shorter, but for performance you should use an array to minimize the interaction with the range: @ShaiRado的答案的确使代码更短,但是为了提高性能,您应该使用数组以最小化与范围的交互:
Option Explicit
Public Sub SetCities()
Const COL_G = 1
Const COL_K = 5
Const COL_L = 6
Dim r As Long, arr As Variant, lastrow2 As Long, ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'read entire range
With ws
lastrow2 = .Cells(.Rows.Count, "L").End(xlUp).Row
arr = .Range("G2:L" & lastrow2)
End With
For r = 1 To UBound(arr)
If Not IsError(arr(r, COL_G)) And Not IsError(arr(r, COL_K)) Then
If Len(arr(r, COL_K)) > 0 Then
Select Case arr(r, COL_G)
Case "SOCHACZEW": arr(r, COL_L) = 31.2
Case "SEKERPINAR", "MECHELEN", "STRANCICE", "KLIPPAN", "MATARO"
arr(r, COL_L) = 33
Case "ATHENS": arr(r, COL_L) = 28
Case "TIMISOARA": arr(r, COL_L) = 34
Case "KIEV", "ITELLA"
arr(r, COL_L) = 32
Case "ROSTOV": arr(r, COL_L) = 32.6
End Select
End If
End If
Next
ws.Range("G2:L" & lastrow2) = arr 'write entire range
End Sub
or at least turn off Application.ScreenUpdating
before execution, and back on after 或至少在执行之前关闭Application.ScreenUpdating
然后在执行之后重新打开
You should also fully qualify all ranges to be explicit about the Worksheet 您还应该完全限定所有范围,以明确了解工作表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.