PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 14, 2022

[FIXED] How to use error handling multiple time in for loop?

 November 14, 2022     automation, error-handling, excel, vba     No comments   

Issue

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??


Solution

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


Answered By - GWD
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing