1
For Each cell In Workbooks("workbook.xlsm").Sheets("sheet1").Range("P5:P12").Cells
sheetname = cell.Value
Windows("new workbook.xlsx").Activate
Worksheets(division).Select
On Error GoTo SKIPCODE

{code}

SKIPCODE:
Next

While running this code if the sheet with name in range of P5-P12 does not exist it gives an error. FOr first such occurrence error is handled and loop continues but for second occurrence code breaks. Can someone help on this??

2

1 Answer 1

1

You have to reset the error handler object after having "handled" the error, in your case by just skipping the iteration. Usually this is done automatically by the resume keyword, however, since you are not using resume because you are essentially just ignoring the error, you have to do it manually by using On Error GoTo -1

Try adding that to your code like this:

For Each cell In Workbooks("workbook.xlsm").Sheets("sheet1").Range("P5:P12").Cells
    sheetname = cell.Value
    Windows("new workbook.xlsx").Activate
    Worksheets(division).Select
    On Error GoTo SKIPCODE

    '{code}

SKIPCODE:
    On Error GoTo -1
Next
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.