是否有更有效的For-Next方法,包括If-Then语句?

时间:2017-10-22 11:15:10

标签: excel vba excel-vba for-loop if-statement

我正在提高两个宏的效率。除了标题上描述的方法之外,我还设法改进了所有方法。它功能齐全,但我确信有一种更好的方法来纠正我在下面提供的代码部分:

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

2 个答案:

答案 0 :(得分:3)

您可以将一些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

答案 1 :(得分:1)

@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

或至少在执行前关闭Application.ScreenUpdating,然后在

之后重新开启

您还应该完全限定所有范围以明确工作表