2

I don't understand why this code has compile error Next without For Please help :(

Sub DefColorCodes()

    For i = 2 To 5
        Range("actReg").Value = Range("Sheet1!A" & i).Value
        ActiveSheet.Shapes.Range("actReg").Select
        With Selection.ShapeRange.Fill.ForeColor.RGB = Range(Range("actRegCode").Value).Interior.Color

    Next i

    Range("B17").Select
End Sub
1
  • 3
    You need an End With before the Next Commented Dec 4, 2014 at 16:00

2 Answers 2

7

You aren't ending your With. Add an End With line at some point before your Next i line.

Sub DefColorCodes()
    For i = 2 To 5
        Range("actReg").Value = Range("Sheet1!A" & i).Value
        ActiveSheet.Shapes.Range("actReg").Select
        With Selection.ShapeRange.Fill.ForeColor.RGB = Range(Range("actRegCode").Value).Interior.Color
        End With
    Next i
    Range("B17").Select
End Sub

In this case it's quite possible that you didn't mean to use a With at all:

Sub DefColorCodes()
    For i = 2 To 5
        Range("actReg").Value = Range("Sheet1!A" & i).Value
        ActiveSheet.Shapes.Range("actReg").Select
        Selection.ShapeRange.Fill.ForeColor.RGB = Range(Range("actRegCode").Value).Interior.Color
    Next i
    Range("B17").Select
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

@MiloKang: Great! If you could accept my answer this will let users know that the solution worked for you. Welcome to Stack Overflow!
2

You are using a With statement inside your for loop with out a End With

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.