sijpie
Well-known Member
- Joined
- Nov 1, 2008
- Messages
- 4,266
- Office Version
- 365
- Platform
- Windows
- MacOS
Run the following code a few times, and then also supply a non existing sheet name. The second time you do that (does not have to be sequentially) the function fails, the on Error will not help out. Why is this happening?
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> GetSheet()<br> <SPAN style="color:#00007F">Dim</SPAN> wsP <SPAN style="color:#00007F">As</SPAN> Worksheet<br> <SPAN style="color:#00007F">Dim</SPAN> vInp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br> <br> <br> vInp = InputBox("Provide 1st SheetName. 0 to quit.", _<br> Title:="Bug test Sheets")<br> <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">While</SPAN> vInp <> 0 And vInp <> vbNullString<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> NotExist<br> <SPAN style="color:#00007F">Set</SPAN> wsP = Sheets(vInp)<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br> <br> <SPAN style="color:#00007F">GoTo</SPAN> NextSheet<br>NotExist:<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br> MsgBox "Invalid sheet name given. Sheet " & vInp & " does not exist."<br>NextSheet:<br> <SPAN style="color:#00007F">Set</SPAN> wsP = <SPAN style="color:#00007F">Nothing</SPAN><br> vInp = InputBox("Enter next SheetName. 0 to quit.", _<br> Title:="Bug test Sheets")<br> <br> <SPAN style="color:#00007F">Loop</SPAN><br> <br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> GetSheet()<br> <SPAN style="color:#00007F">Dim</SPAN> wsP <SPAN style="color:#00007F">As</SPAN> Worksheet<br> <SPAN style="color:#00007F">Dim</SPAN> vInp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br> <br> <br> vInp = InputBox("Provide 1st SheetName. 0 to quit.", _<br> Title:="Bug test Sheets")<br> <SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">While</SPAN> vInp <> 0 And vInp <> vbNullString<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> NotExist<br> <SPAN style="color:#00007F">Set</SPAN> wsP = Sheets(vInp)<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br> <br> <SPAN style="color:#00007F">GoTo</SPAN> NextSheet<br>NotExist:<br> <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br> MsgBox "Invalid sheet name given. Sheet " & vInp & " does not exist."<br>NextSheet:<br> <SPAN style="color:#00007F">Set</SPAN> wsP = <SPAN style="color:#00007F">Nothing</SPAN><br> vInp = InputBox("Enter next SheetName. 0 to quit.", _<br> Title:="Bug test Sheets")<br> <br> <SPAN style="color:#00007F">Loop</SPAN><br> <br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>