1

I have a lite problem with some statement in my code. the situation is like this: I am working with UserForm in excel based on selection of months and the data goes to my report from another report. To analyze data of certain month we need data of the month before. So for January we need data from last's year database for December. I made special sub just in case we need to analyze data about January and we need the old database too. And with that statment I have a problem and I get that error "VBA End If without block If Error" :

'If the month that was selected is January then we need to open old Visual file Month
If MonthName = Lists.Range("A2").Value Then 'it means January
    Call January
Else
    For Each ws In Worksheets
        If ws.Name = "December" Then
            Sheets("December").Delete
            End
        End If
    Next ws
End If

Please, can someone help?

5
  • Remove this End before the End If Commented Nov 26, 2017 at 15:09
  • 1
    If you mean to Exit then put Exit Sub Commented Nov 26, 2017 at 15:10
  • I dont want to exit, just to stop the loop if I found worksheet named "December". @QHarr Commented Nov 26, 2017 at 15:12
  • 2
    then put Exit For Commented Nov 26, 2017 at 15:13
  • BTW - It is not a good idea to use VBA function names (e.g. MonthName) as variable names. (I don't think that would cause the "End If without block If" error though - that sounds more like one of your If statements was written as a single-line If but you still wrote an End If for it, but your posted code doesn't show that.) Commented Nov 26, 2017 at 21:01

1 Answer 1

8

Use Exit For to exit a For Loop.

If MonthName = Lists.Range("A2").Value Then 'it means January
    Call January
Else
    For Each ws In Worksheets
        If ws.Name = "December" Then
            ws.Delete
            Exit For
        End If
    Next ws
End If
Sign up to request clarification or add additional context in comments.

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.