如果满足IF条件,写"字符串"并转到下一个

时间:2015-12-18 11:02:35

标签: excel vba excel-vba

我有这一行:

If UCase(Sheets("DebitCard_Check").Range("G" & i).Value) Like "*TAX*" Then GoTo Skip1

我需要的是插入这样的东西:

IF condition is met, write "string" Then GoTo Skip1

如果不满足条件,上面应继续正常执行其余代码。

我应该如何处理语法?

我尝试了一些东西。否则,ElseIF总是将对象填充为:

(Sheets("DebitCard_Check").Range("G" & i).Value) = "string"

它给了我多个错误。 没有IF的EndIF 预期的陈述,对象,行等

*********************************编辑1 ************ ************************

这是整个代码 - 修改后的建议:

Sub Categories_Update()
    Dim lastrow As Long, lastrow2 As Long
    Dim i As Integer, j As Integer
    Dim PatternFound As Boolean

    Call speedup

    lastrow = Sheets("Rules").Range("A" & Rows.Count).End(xlUp).Row
    lastrow2 = Sheets("DebitCard_Check").Range("L" & Rows.Count).End(xlUp).Row

    For i = 4 To lastrow2

    ' *** This is where we can insert, a conditional that causes the row to get skipped based on
    'column G being "CYRRILIC TEXT" - aka Auto Tax - this conditional sends it to the part "Skip1" down below

        If UCase(Sheets("DebitCard_Check").Range("G" & i).value) Like "*TAX*" Then
            Sheets("DebitCard_Check").Range("M" & i).value = "Automatic Tax"
            GoTo Skip1

            PatternFound = False

            j = 1


        Do While PatternFound = False And j < lastrow
            j = j + 1
            If UCase(Sheets("DebitCard_Check").Range("L" & i).value) Like "*" & UCase(Sheets("Rules").Range("A" & j).value) & "*" Then
                Sheets("DebitCard_Check").Range("M" & i).value = Sheets("Rules").Range("C" & j).value
                PatternFound = True
            End If
        Loop

Skip1:

    Next i

Call normal

End Sub

Public Sub speedup()
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
End Sub

Public Sub normal()
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
End Sub

不幸的是,这给了我一个错误 - &gt; 编译错误:下一步没有

1 个答案:

答案 0 :(得分:1)

这就是你应该这样做的方式:

If UCase(Sheets("DebitCard_Check").Range("G" & i).value) Like "*TAX*" Then
    Sheets("DebitCard_Check").Range("G" & i).value = "string"
    GoTo Skip1
End If

<强> [编辑]

您应该在代码中进行适当的缩进 - 找到这样的错误会更容易。您忘了关闭For ... Next声明。

我在代码中发现了另一个错误。你应该移动这两行:

PatternFound = False
j = 1

GoTo Skip1标签之前。否则他们永远不会被执行。

以下是纠正所有错误的代码:

Sub Categories_Update()
    Dim lastrow As Long, lastrow2 As Long
    Dim i As Integer, j As Integer
    Dim PatternFound As Boolean

    Call speedup

    lastrow = Sheets("Rules").Range("A" & Rows.Count).End(xlUp).Row
    lastrow2 = Sheets("DebitCard_Check").Range("L" & Rows.Count).End(xlUp).Row

    For i = 4 To lastrow2

        ' *** This is where we can insert, a conditional that causes the row to get skipped based on
        'column G being "CYRRILIC TEXT" - aka Auto Tax - this conditional sends it to the part "Skip1" down below

        If UCase(Sheets("DebitCard_Check").Range("G" & i).value) Like "*TAX*" Then
            Sheets("DebitCard_Check").Range("M" & i).value = "Automatic Tax"

            'NOTE: Those two lines must be before [GoTo Skip1]. Otherwise, ----------|
            'they won't be executed.                                                '|
            PatternFound = False                                                    '|
            j = 1                                                                   '|
            '------------------------------------------------------------------------|

            GoTo Skip1

        End If

        Do While PatternFound = False And j < lastrow
            j = j + 1

            If UCase(Sheets("DebitCard_Check").Range("L" & i).value) Like "*" & UCase(Sheets("Rules").Range("A" & j).value) & "*" Then
                Sheets("DebitCard_Check").Range("M" & i).value = Sheets("Rules").Range("C" & j).value
                PatternFound = True
            End If

        Loop
Skip1:

    Next i

    Call normal

End Sub

Public Sub speedup()
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
End Sub

Public Sub normal()
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
End Sub