Oleksandr  Titorchuk
Oleksandr Titorchuk

Reputation: 69

Using "For-Next" inside "If" (Excel, VBA)

I am trying to combine multiple "If"s and one "For-Next" statement inside a single "If" (code follows). Unfortunately I am always get an error message: "Compile Error: Else without If". Can you look at my code for mistakes?

Sub Debugging()

Workbooks("Problem.xls").Worksheets(1).Activate

Cash_Rows = 5
Share_Rows = 6

If Cash_Rows <= Share_Rows Then

    Range("A1:A" & Cash_Rows).Select
    With Selection.Interior
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0.399975585192419

    Count_Cash = Application.WorksheetFunction.CountIf(Range("A:A"), "L*")

    For Each cell In Range("A1:A" & Cash_Rows)
        If CStr(cell.Value) Like "L*" Then
        Range("A" & cell.Row & ":" & "D" & cell.Row).Interior.Color = 65535
        Dim Index As Integer
        Index = Application.WorksheetFunction.Match(CStr(cell.Value), Range("F2:" & "F" & Share_Rows), 0)
        Range("F" & Index & ":" & "I" & Index).Interior.Color = 65535
        End If
    Next

    If Count_Cash = 0 Then
        MsgBox "You do not have any matching ID+Amount between Cash and Shares booking. It's OK!"
    Else
        MsgBox "You have " & Count_Cash & " matching transactions. Check them!"
    End If

Else

MsgBox "Do not worry. Be happy!"

End If
End Sub

Thank you in advance for help!

Upvotes: 0

Views: 500

Answers (1)

Tim Wilkinson
Tim Wilkinson

Reputation: 3801

You need to end the with.

With Selection.Interior
    .ThemeColor = xlThemeColorAccent6
    .TintAndShade = 0.399975585192419
End With

Upvotes: 1

Related Questions