AutoFilterQueen
Board Regular
- Joined
- Sep 28, 2011
- Messages
- 90
I have recently had 3 instances where my error handling is not working the way I had intended. I am not sure whether these instances are related or three different problems. I have used error handling many times before and it has worked the way I intended it, so I am hoping this is not just my complete lack of understanding the concept!!! I am currently running Excel 2007 but had previously used this code with Excel 2003 and it worked the way I had intended with Excel 2003.
Instance 1:
This subroutine produces the error message “Run-time error '91': Object variable or With block variable not set” and the offending line of code is the one sandwiched between the error handling lines. This subroutine did not produce an error in Excel 2003 (which may or may not be relevant). I have since found a better way to programmatically clear the clipboard (http://www.cpearson.com/excel/Clipboard.aspx) but I still wonder why the On Error Resume Next does not result in ignoring the error in this line of code.
Instance 2:
Once again, this code produces the error message “Run-time error '91': Object variable or With block variable not set” and the offending line of code is the one sandwiched between the error handling lines. In Excel 2003, this did go to nonefound.
Instance 3:
This code produces the error message Run-time error '1004': ShowAllData method of Worksheet class failed and the offending line of code is the one sandwiched between the error handling lines. In Excel 2003, if the ShowAllData method failed it ignored the error and proceeded to the next line of code. Is there some difference between error handling in Excel 2003 and Excel 2007? I really need to make error handling work for me in instances 2 and 3. I was able to get around it in instance 1 by finding another way to clear the clipboard, but in instances 2 and 3 I expect there to be errors and for good reason, and I want to handle them appropriately. Any insight would be greatly appreciated!
Instance 1:
Code:
Public Sub ClearClipboard()
On Error Resume Next
Application.CommandBars("Clipboard").FindControl(, 3634).Execute
On Error GoTo 0
End Sub
This subroutine produces the error message “Run-time error '91': Object variable or With block variable not set” and the offending line of code is the one sandwiched between the error handling lines. This subroutine did not produce an error in Excel 2003 (which may or may not be relevant). I have since found a better way to programmatically clear the clipboard (http://www.cpearson.com/excel/Clipboard.aspx) but I still wonder why the On Error Resume Next does not result in ignoring the error in this line of code.
Instance 2:
Code:
On Error GoTo nonefound
comparesheet.Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=True).Activate
On Error GoTo 0
Once again, this code produces the error message “Run-time error '91': Object variable or With block variable not set” and the offending line of code is the one sandwiched between the error handling lines. In Excel 2003, this did go to nonefound.
Instance 3:
Code:
On Error Resume Next
activefile.Sheets("Raw Data").ShowAllData
On Error GoTo 0
This code produces the error message Run-time error '1004': ShowAllData method of Worksheet class failed and the offending line of code is the one sandwiched between the error handling lines. In Excel 2003, if the ShowAllData method failed it ignored the error and proceeded to the next line of code. Is there some difference between error handling in Excel 2003 and Excel 2007? I really need to make error handling work for me in instances 2 and 3. I was able to get around it in instance 1 by finding another way to clear the clipboard, but in instances 2 and 3 I expect there to be errors and for good reason, and I want to handle them appropriately. Any insight would be greatly appreciated!