用于if / and / or语句的循环

问题描述 投票:0回答:4

我有以下代码:

Sub CreateDisableLists()

Dim LastRow As Long
Dim i As Long
LastRow = Cells(Rows.Count, "J").End(xlUp).Row
For i = 2 To LastRow
    If _
       Range("G" & i).Value = "DSDFDFFD" And Range("I" & i).Value = "Enabled" Or _
       Range("G" & i).Value = "SFDDS" And Range("I" & i).Value = "Enabled" Or _
       Range("G" & i).Value = "FFDFDSSF" And Range("I" & i).Value = "Enabled" Or _
       Range("G" & i).Value = "FDFDSVSFD" And Range("I" & i).Value = "Enabled" Or _
       Range("G" & i).Value = "FDFDSFD" And Range("I" & i).Value = "Enabled" Or _
       Range("G" & i).Value = "GHFH" And Range("I" & i).Value = "Enabled" _
    Then
       Range("K" & i).Value = "TRUE"
    Else
       Range("K" & i).Value = "FALSE"
    End If
Next i

End Sub

如何压缩“ If”和“ Then”之间的行,以便遍历(DSDFDFFD,SFDDS,FFDFDSSF等)列表而不是上面的内容?使用此代码,我需要添加68行在“如果”和“然后”之间。

excel vba
4个回答
0
投票

如果使用多个Or / And语句,我强烈建议您使用圆括号对它们进行分组,以使它们得到验证,否则您可能无法获得预期的结果。

您的If语句可能类似于:

If Left$(Range("G" & i).Value, 3) = "xxx" And _
   Val(Right(Range("G" & i).Value, 1)) >= 1 And _
   Val(Right(Range("G" & i).Value, 1)) <= 6 And _
   Range("I" & i).Value = "Enabled" _
Then
    Range("K" & i).Value = "TRUE"
Else
    Range("K" & i).Value = "FALSE"
End If

0
投票

没有太多改进,但是下一个代码会更紧凑一些:

Sub testImproveCode()
    Dim LastRow As Long, i As Long
    Dim j As Long, boolOk As Boolean
    LastRow = Cells(Rows.count, "J").End(xlUp).Row
    For i = 2 To LastRow
        For j = 1 To 6
            If Range("I" & i).value = "xxx" & j And _
                   Range("I" & i).value = "Enable" Then
                boolOk = True: Exit For
        Next j
        If boolOk Then
             Range("K" & i).value = "TRUE": boolOk = False
        Else
             Range("K" & i).value = "FALSE"
        End If
    Next i
End Sub

0
投票

您可以先将K设置为FALSE,然后在I列使用If,在G列使用Select Case

Sub sCreateDisableLists()
    Dim LastRow As Long
    Dim i As Long
    LastRow = Cells(Rows.Count, "J").End(xlUp).Row
    For i = 2 To LastRow
        Range("K" & i).Value = "FALSE"
        If Range("I" & i).Value = "Enabled" Then
            Select Case Range("G" & i).Value
                Case "xxx1", "xxx2", "xxx3", "xxx4", "xxx5", "xxx6"
                    Range("K" & i).Value = "TRUE"
            End Select
        End If
    Next i
End Sub

0
投票

您可以尝试:

Option Explicit

Sub CreateDisableLists()

    Dim LastRow As Long, i As Long, y As Long
    Dim strValues As String: strValues = "DSDFDFFD,SFDDS,FFDFDSSF,FDFDSVSFD,FDFDSFD,GHFH"
    Dim strIvalue As String: strIvalue = "Enabled"
    Dim arr As Variant
    Dim BooleanStatus As Boolean

    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .Cells(.Rows.Count, "J").End(xlUp).Row

        arr = Split(strValues, ",")

        For i = 2 To LastRow

            BooleanStatus = False

            For y = LBound(arr) To UBound(arr)

                If (.Range("G" & i).Value = arr(y)) And .Range("I" & i).Value = strIvalue Then
                    BooleanStatus = True
                    Exit For
                End If

            Next y

            If BooleanStatus = True Then
                .Range("K" & i).Value = "TRUE"
            Else
                .Range("K" & i).Value = "FALSE"
            End If

        Next i

    End With

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.