Error handling only works once

时间:2016-02-12 22:03:23

标签: vba excel-vba excel

I have a very simple VBA code that should try to open a nonexistent file, send me to an error handler, then back to my code in an infinite loop (intentionally).

However, the compiler only catches the error the first time, then breaks on the second pass.

I have tried every combination of On Error statements to send it back on the second pass, but nothing seems to work. Here is the code:

$tor = file('ips_tor.csv');

if(in_array_r('1.34.163.57', $tor))
   echo '<span style="color: red;display: inline-block; margin-right: 20px;">OK</span>';

else
   echo '<span style="color: black; display: inline-block; margin-right: 20px;">Not Ok</span>';

Is there something I'm missing, or is this a bug?

Don't test this unless you are at peace with having to kill Excel, or add an extra condition for looping

2 个答案:

答案 0 :(得分:3)

Use public static Set<Map.Entry<MultiKey<? extends String>, String>> match2Keys(String first, String second, MultiKeyMap<String, String> multiKeyMap) { Set<Map.Entry<MultiKey<? extends String>, String>> set = new HashSet<>(); for (Map.Entry<MultiKey<? extends String>, String> entry : multiKeyMap.entrySet()) { if (first.equals(entry.getKey().getKey(0)) && second.equals(entry.getKey().getKey(1))) { set.add(entry); } } return set; } to... well... resume processing after the error handler.

Resume

I have used code like this to access a certain worksheet, if it is not found then the error handler creates one with the correct name and then resumes the processing.

答案 1 :(得分:1)

You need to clear the error message by using:

FRAME_CALL_THRESHOLD

Source: https://msdn.microsoft.com/en-us/library/hh2zczch(v=vs.90).aspx

Basically it still thinks you're handling the error, and hasn't reset the handling mechanism, so it doesn't re-trigger until you clear it.

(Resume also clears the Error as well, as you can note by the reference above).