473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

vba Error handler for Excel - make it close on error

Hi, I asked yesterday about this - but maybe my title was bad. No one responded.

I have a refreshable Excel Spreadsheet that pulls data from an Access Database on the server. The server has been a pain lately - and not letting anything finish and causing lots of time outs.

I want to put an error handler into my vba macro that will force the application to quit if any errors occur.

I am thinking it is simply this:

sub name

on error goto Myerrorhandler

'rest of macro

Myerrorhandler:
application.quit

end sub


Is this right? I work remotely so I am not able to kill a job that gets hung - so i want to make sure my code is right before i do it.

Thanks for your help!

Sophie
Oct 23 '07 #1
7 17595
it should be:
Expand|Select|Wrap|Line Numbers
  1. Application.DisplayAlerts = False
  2. Application.ActiveWorkbook.Close
  3. Application.DisplayAlerts = True
  4.  
Oct 23 '07 #2
it should be:
Expand|Select|Wrap|Line Numbers
  1. Application.DisplayAlerts = False
  2. Application.ActiveWorkbook.Close
  3. Application.DisplayAlerts = True
  4.  
so i put your code in under Myerrorhandler? instead of my code?
Oct 24 '07 #3
kadghar
1,295 Expert 1GB
so i put your code in under Myerrorhandler? instead of my code?
application.quit
in a vba code will close your excel, acces or the office application you're working with.

application.displayalerts = false
will make the alerts like "do you want to save changes?" no to show

so when you write activeworkbook.close it will close the active book you have in excel without asking.

I dont know if what you mean with "application" is just the macro or excel or acces. If its the macro, an "exit sub" will do. If you have a UserForm, you'll have to quit it before exiting the sub.

HTH
Oct 24 '07 #4
kadghar
1,295 Expert 1GB
i forgot to tell you.

If you want the easy way (case u're running a macro)

End

Will do
Oct 24 '07 #5
i forgot to tell you.

If you want the easy way (case u're running a macro)

End

Will do

Hi, thanks for the info. I do infact want the whole application to close. We have these reports sitting on a desktop and on a scheduler - so they run every hour. When they are finished refreshing - they close so the next one can run. Problem we are having is if one gets stuck due to an ODBC error - the application won't quit - and the next inline doesn't get to run. (or at least that is how it seems to be working) The other ones try to open - but because of the ODBC Error the macro's don't appear to run...so they all just sit there in limbo.

My actual code is as follows:
Expand|Select|Wrap|Line Numbers
  1. Sheets("R30 Hours Detail").Select
  2.     Range("A2").Select
  3.     Selection.QueryTable.Refresh BackgroundQuery:=False
  4.     Sheets("R30 Hours Summary").Select
  5.     Range("A3").Select
  6.     ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
  7.     Sheets("R30 Detail").Select
  8.     Range("A2").Select
  9.     Selection.QueryTable.Refresh BackgroundQuery:=False
  10.     Sheets("R30 Compliance").Select
  11.     Range("A3").Select
  12.     ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
  13.     Sheets("R31 Hours Detail").Select
  14.     Range("A2").Select
  15.     Selection.QueryTable.Refresh BackgroundQuery:=False
  16.     Sheets("R31 Hours Summary").Select
  17.     Range("A3").Select
  18.     ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
  19.     Sheets("R31 Detail").Select
  20.     Range("A2").Select
  21.     Selection.QueryTable.Refresh BackgroundQuery:=False
  22.     Sheets("R31 Compliance").Select
  23.     Range("A3").Select
  24.     ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
  25.     Sheets("R32 Hours Detail").Select
  26.     Range("A2").Select
  27.     Selection.QueryTable.Refresh BackgroundQuery:=False
  28.     Sheets("R32 Hours Summary").Select
  29.     Range("A3").Select
  30.     ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
  31.     Sheets("R32 Detail").Select
  32.     Range("A2").Select
  33.     Selection.QueryTable.Refresh BackgroundQuery:=False
  34.     Sheets("R32 Compliance").Select
  35.     Range("A3").Select
  36.     ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
  37.     ActiveWorkbook.Save
  38.     Application.Quit
  39.  
  40.     End Sub
  41.  
That was built by someone else.

I think that we should be able to use something simpler like this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2.  
  3.      On Error GoTo ErrorHandler
  4.  
  5.           ActiveWorkbook.RefreshAll
  6.  
  7.           ActiveWorkbook.Save
  8.  
  9.           Application.Quit
  10.  
  11.      ErrorHandler:
  12.             Application.Quit
  13.  
  14. End Sub
  15.  
  16.  


instead of the way it is - but I could be wrong. (so far the 2 reports I set up like this w/out the error handler part - don't always run, but I am not entirely convinced that it is the report - i am thinking it has something to do with the server timing out - and the original way - does make it so you refresh one data source at a time - but there is still no error handling - so it crashes too)

I tried adding a resume function in for the error handler...and that was a nighmare I don't wish to repeat.

Anyway...if you have any suggestions...I'd really appreciate it.

Thanks!
Sophie
Oct 26 '07 #6
kadghar
1,295 Expert 1GB
I think that we should be able to use something simpler like this:
...
instead of the way it is - but I could be wrong. (so far the 2 reports I set up like this w/out the error handler part - don't always run, but I am not entirely convinced that it is the report - i am thinking it has something to do with the server timing out - and the original way - does make it so you refresh one data source at a time - but there is still no error handling - so it crashes too)

I tried adding a resume function in for the error handler...and that was a nighmare I don't wish to repeat.

Anyway...if you have any suggestions...I'd really appreciate it.

Thanks!
Sophie
The first code you posted was made with the macro recorder, so there's always a way to make it simpler. The error handler is a great idea. But if you want to have more control of the errors, i'd recomend you to use

On Error Resume Next

(this will just ignore the code lines with an error)

and at the end of the code, put something like

If Err.Num = [The number of the error you want to detect] then
exit sub 'Or whatever you want to do
end if

you can make a SELECT CASE Err.Num

Now, the problem here is that Err.Num is the number of the last error, so if you had many errors during the execution of the code, you wont be able to see the first ones.
Oct 26 '07 #7
The first code you posted was made with the macro recorder, so there's always a way to make it simpler. The error handler is a great idea. But if you want to have more control of the errors, i'd recomend you to use

On Error Resume Next

(this will just ignore the code lines with an error)

and at the end of the code, put something like

If Err.Num = [The number of the error you want to detect] then
exit sub 'Or whatever you want to do
end if

you can make a SELECT CASE Err.Num

Now, the problem here is that Err.Num is the number of the last error, so if you had many errors during the execution of the code, you wont be able to see the first ones.

Thnks a lot... It helped me in fixing my problem...
Jan 3 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: g_mx6 | last post by:
I am not sure what I am doing wrong. I am trying to open a DSNLess connection and retrieve the data in an Excel sheet using ASP. Shouldnt be this complicated but I've been trying to get this to...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
2
by: Steve Richfield | last post by:
My error handler works GREAT. However, VBA seems to have some bugs/features that are causing it fits. The little snippet that I put at the end of each routine looks like this: Error_Handler: If...
3
by: deko | last post by:
I have a logging routine that's supposed to silently log errors caught by error handler code on certain functions. The problem is sometimes stuff happens and the error handler can get caught in a...
10
by: robert d via AccessMonster.com | last post by:
I have a global error handler that up until today has been working flawlessly. Let me first provide the relevant code **************************************************************** On Error...
2
by: Jukka Aho | last post by:
When converting Unicode strings to legacy character encodings, it is possible to register a custom error handler that will catch and process all code points that do not have a direct equivalent in...
2
by: yeghia \(sosy\) | last post by:
Hi Guys I have two questions. 1. Is there a way to set a handler to .NET application so that in case of not caught exception my handler is called before application shutdown. I want to send an...
8
by: g_man | last post by:
I am trying trap Runtime error 3022 (duplicates) in the click event of a command button that closes the form. I have code in the Form_Error event that does a good job of providing a more meaningful...
2
by: LadyRed2 | last post by:
Hello, I am new to Excel and SQL server, so please be patient with me. I have written code with the help of others to update a table in a sql database from an excel form. The connection code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.