【问题标题】:Is there a more efficient method for For-Next including If-Then statement?For-Next 是否有更有效的方法,包括 If-Then 语句?
【发布时间】:2018-04-03 01:26:18
【问题描述】:

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

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

【问题讨论】:

  • 你可以用Select Case cell2.Offset(0, -5).Value代替你的多个ElseIfs
  • 所以,我应该将 ElseIf 替换为 Select Case cell2.Offset(0, -5).Value ?
  • 在下面查看我的答案和代码,看看是否对您有帮助

标签: vba excel for-loop if-statement


【解决方案1】:

您可以将几个ElseIfs 组合在一起,因为它们共享相同的结果,再加上切换到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

【讨论】:

  • 非常感谢!所以,在您看来,您的代码尽可能高效,对吧?
  • @PericlesFaliagas 与您提供的当前数据是的。
  • 还有一个问题...如果我没有 Elseifs 具有相同的数据,是否值得使用这种方法?我想说的是,这种方法通常是否比我使用的方法更有效
  • @PericlesFaliagas 不确定,很可能你可以谷歌多个ElseIfSelect Case 之间的基准性能,我只是发现代码“更干净”并且更容易调试(如果你想添加未来更多场景)
【解决方案2】:

@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,然后再打开

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多