On Error Goto Msgbox Vba
Contents |
Forums Excel Questions vba: if error show msgbox Results 1 to 6 of 6 vba: if error show msgboxThis is a discussion on vba: if error show msgbox within the Excel Questions vba on error msgbox forums, part of the Question Forums category; I have a subroutine that opens
On Error Goto Vba
a file based on some input from the user.... generally the file it looks ... LinkBack LinkBack URL About LinkBacks vba error handling best practices Bookmark & Share Digg this Thread!Add Thread to del.icio.usBookmark in TechnoratiTweet this thread Thread Tools Show Printable Version Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Oct 17th, 2006,08:17 PM #1
On Error Goto Line
cmhoz Board Regular Join Date Aug 2006 Location Australia Posts 268 vba: if error show msgbox I have a subroutine that opens a file based on some input from the user.... generally the file it looks for has a standard naming format used. However sometimes the person responsible for this will use some other name, thus screwing up my macro. Rather than excel showing the generic error box, try catch vba confusing the user completely, I want it to show MY msgbox telling the user to go in and find the file manually... I'm sure I've done this before, but I'm drawing a blank on how I managed it. Any help out there?? Sub openphiac() Dim strfolder as string Dim strphiacfile as string strfolder = Range("folder") strphiacfile = Range("phiacfile") Workbooks.Open Filename:="O:\Phiac Data\PhiacTables\" & strfolder & "\" & strphiacfile & ".xls" End Sub Share Share this post on Digg Del.icio.us Technorati Twitter Reply With Quote Oct 17th, 2006,08:25 PM #2 HalfAce MrExcel MVP Join Date Apr 2003 Location Alaska Posts 9,444 Hi cmhoz Something like this ought to do it. Code: Sub openphiac() Dim strfolder As String Dim strphiacfile As String strfolder = Range("folder") strphiacfile = Range("phiacfile") On Error GoTo ErrMsg Workbooks.Open Filename:="O:\Phiac Data\PhiacTables\" & strfolder & "\" & strphiacfile & ".xls" Exit Sub ErrMsg: MsgBox ("Type in your message here."), , "MESSAGE TITLE" End Sub Share Share this post on Digg Del.icio.us Technorati Twitter Reply With Quote Oct 17th, 2006,08:26 PM #3 Macropheliac Board Regular Join Date Aug 2005 Posts 165 I can't test it, but maybe this: Code: Sub openphiac() On Error GoTo Errormask Dim strfolder As String Dim strphiacfile As String
generally put more focus on the coding part and getting the desired result but during this process we forget an important thing i.e. Error handling. Error handling is an important part of every code and VBA On Error Statement is
Vba On Error Exit Sub
an easy way for handling unexpected exceptions in Excel Macros. A well written macro is
Vba On Error Goto 0
one that includes proper exception handling routines to catch and tackle every possible error. Error handling is important because in case of any unexpected vba error handling in loop exceptions your code doesn’t break. Even if any fatal unexpected error occurs in the code then also you should ensure that the code should terminate gracefully. Definition of VBA On Error Statement: On Error statement instructs VBA Compiler, what http://www.mrexcel.com/forum/excel-questions/229230-visual-basic-applications-if-error-show-msgbox.html to do in case any runtime exception are thrown. Syntax of On Error Statement: Basically there are three types of On Error statement: On Error Goto 0 On Error Resume Next On Error Goto
VBA Code & Other Help Excel Help MsgBox used during On Error Resume Next ? If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register http://www.vbaexpress.com/forum/showthread.php?39786-MsgBox-used-during-On-Error-Resume-Next link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. Results 1 to 6 of 6 Thread: MsgBox used during On Error Resume Next ? Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 11-12-2011,07:26 PM #1 bdsii View Profile View Forum Posts View Blog Entries View Articles VBAX Contributor Joined Jul 2009 Posts on error 157 Location MsgBox used during On Error Resume Next ? Hello all....I have simple code that is used by other users. I would like to have a custom MsgBox appear if an error occurs that would help the user to let me know where the error occured but then continue on without stopping the code. I know this is not preferred in most cases but in my situation, not running the code causing the errors is on error goto better than stopping the entire process. What I would like is code something like this but I am not sure if this will work and not sure how to test error code realtime. I also would like to be able to customize the msgbox to show a different message at different points in the code. On Error Resume Next MsgBox "Error when trying to _________" 'continue on with code at point error occurred Formatting tags added by mark007 So...what is the correct format for this example and can I repeat it with differing messages later in the code ? Thanks in advance. Reply With Quote 11-12-2011,07:37 PM #2 mikerickson View Profile View Forum Posts View Blog Entries View Articles Mac Moderator VBAX Guru Joined May 2007 Location Davis CA Posts 2,667 Using that structure, you would customize as you go '...some code On Error Resume Next '... potentially erroring code If Err Then MsgBox Error & " occured at the first point" Err.Clear '... more potentilly erroring code If Err Then MsgBox Error & " occured at the second code" Err.Clear '... more code On Error Goto 0 Formatting tags added by mark007 Reply With Quote 11-13-2011,04:38 AM #3 mdmackillop View Profile View Forum Posts View Blog Entries View Articles AdministratorVP-Knowledge Base VBAX Grand Master Joined May 2004 Location Scotland Posts 12,873 Location Add