+ Reply to Thread
Results 1 to 4 of 4

Workbook Merging and Err Handling

  1. #1
    Registered User
    Join Date
    05-07-2014
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    15

    Workbook Merging and Err Handling

    I got this great sub from Ron de Bruin that does everything I need (Almost). The merging function works wonderfully the only thing i have left to figure out is how to add some error handling if a file in the path folder is unable to be opened.

    To elaborate, while i was testing the code one of the files ("DetailReport(2)") was able to merge with the others. Upon further investigation the cause was not with the code but in fact when it was downloaded the file was somehow corrupted.
    What I would like in someway to not only stop the code but possibly display a messagebox with the file name that couldnt be opened. Any ideas??

    Please Login or Register  to view this content.

  2. #2
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Workbook Merging and Err Handling. Errections and Arousing Exceptional Situtions. ;)

    Hi BARENTINEMATT,
    Do you still need help on this one?
    On the face of it your requirement sounds very easy to fulfil.

    But I am having difficulty understanding you?
    _1 ) Have you got some Typos here ?
    Quote Originally Posted by BARENTINEMATT View Post
    ., wh..ile i was testing the code one of the files ("DetailReport(2)") was able to merge with the others. Upon further investigation the cause was not with the code but in fact when it was downloaded the file was somehow corrupted.....
    _ 2 ) You have given a large code snippet, but not the complete code. ( unless you just missed the first Sub bit ). You have not said specifically where you want the extra error handling. I assume it is where the Files are opened.

    I do not recognise that code unfortunately so it is a bit difficult coming in to look at it without a better description from you of what it does.
    But it appears that if a Workbook cannot be opened, then currently the code then ignores trying to do anything with it and just goes on to the next Workbook. So again, you description of the problem is very confusing ?? You say “a file was able to merge , the cause of which was that it was corrupt, " and then what you say suggests it was not opened because it was corrupt”. So that is all nonsense or a typo ??

    There is no point in me trying to modify the code , as it is not clear what you want.
    For example , after getting the message with the File name that could not be opened, what should happen then?
    _ Should the code continue or stop. Currently the code just ignores Files that cannot be opened and goes on to the next File.
    If you wanted the code to continue, then you would have a very simple code line to add just to tell you that file X was not opened.
    _ If the code should end after not opening a file then you modify the code to include an error handling code section, typically towards the end of the code. You then arrange for the code to go there on an Error. There your message would be given just before the code ends.


    Both modifications are very simple. You need I think to be a bit clearer on describing the problem and what you want.

    Alan

    p.s.

    Let me give some background info that may help you to solve the problem yourself.

    On Error Resume Next
    This is a user defined Error handler which overrides the default error handler. It actually suppresses an error situation being raised, and instructs the program to continue just after the line which would have raised an “Error Situation”. An “Error Situation” , sometimes called an Exception, or Error exception , or an Erected Exceptional VBA state, etc..etc.. is a situation in which VBA is “thinking” something is wrong. It behaves in such a situation differently. For example it ignores any further attempts by you to define a new Error Handler. It also ignores your attempts to “switch off” or “switch back” to the default error handler. In the case of using On Error Resume Next Error handler Statement, VBA has been prevented from being aroused to its Erected exceptional state of being. Because of this suppression, you will often see the Error Handler Statement On Error Resume Next being accompanied close by a further Error Handling Statement, On Error GoTo 0( One might use also an On Error GoTo Label user defined error handler instead here, assigning a different user defined error handler. - Again this is possible because VBA has been prevented from being aroused to its Erected exceptional state of being. )
    On Error GoTo 0
    This Error handler Statement “goes” nowhere. It “switches off” the current user defined or “switches back” to the default error handler. It would normally only work if no Error had been encountered. But in the case of using On Error Resume Next it will work. This is because, as noted, On Error Resume Next has surprised the Arousal
    On Error GoTo Label
    This Error handling Statement does “go somewhere!”. It “goes” to the code section labeled Label: ( One can also replace the label with a code line, if you have that code line in your code ). In this case there is no suppression of the aroused Erected Exceptional VBA state. VBA is now “thinking” something is wrong. It behaves in such a situation differently. For example it ignores any further attempts by you to define a new Error Handler. It also ignores your attempts to “switch off” or “switch back” to the default error handler. However, this error handler allows the code to continue, ( rather than stopping as is the case by the default error handler ) , at the point the code “goes to”. Typically things are done there that should be done on the case of an error.
    Object Err
    There is an Object Err available to us. This has a couple of Methods and some Properties. These Properties are strings of information about the last type of error that was encountered. So this can be used, for example, as an indication of if an error occurred after using On Error Resume Next . Note however, If one is wanting to utilize this information in the case of a On Error Resume Next, then one should do that before an On Error GoTo Label or a On Error GoTo 0 . This is because, these two Error Handling Statements Clear that information. ( This clearing can also be done by a code line of Err.Clear )









    http://excelmatters.com/2015/03/17/on-error-wtf/
    https://app.box.com/s/8zkhjcmbxrqnlnexqpktuy41clgqm4zo
    Last edited by Doc.AElstein; 10-17-2016 at 07:19 AM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  3. #3
    Registered User
    Join Date
    05-07-2014
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    15
    Sorry for not being specific enough, that night was a long one. I got that code from Ron de Bruin's site and basically it takes all the workbooks located in the specified folder, usually 40+ workbooks and merges them into 1 worksheet. The issue was that one of the files i was trying to merge was corrupted so i didnt get that data and had i not been testing the code at the time i probably wouldnt have caught it. So my question is how can i word the error handling to exit sub and display a msgbox with the filename that failed instead of the current resume next method. Im just trying to make this code as fail proof as possible since the files im merging are payroll related.
    I may need to post a new thread but additionally i would like to edit it somehow to copy the header of just the first workbook but not rest i initially though it would be pretty simple but i cant seem figure out a good starting piont.

    I appreciate you taking the time to help me out and i hope i explained it well enough this time. Kind of new to the forum world and this is my most advanced macro to date.

  4. #4
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Handling an Erection while still letting a VBA code run... Oh er missus ... ;)

    Hi BARENTINEMATT,
    It is probably a good idea to get in the habit of
    _1 ) giving the URL link to any site that you use, in this case the Ron de Bruin site You got the code
    From
    -2) post a full working code, a shortened version if possible. In this case probably the full code you are using would be OK..
    _....................
    Quote Originally Posted by BARENTINEMATT View Post
    .my question is how can i word the error handling to exit sub and display a msgbox with the filename that failed instead of the current resume next method. ...
    That is fairly easy, I tend to put that sort of error handling as standard on most of my codes..
    Quote Originally Posted by Doc.AElstein View Post
    .....
    _ If the code should end after not opening a file then you modify the code to include an error handling code section, typically towards the end of the code. You then arrange for the code to go there on an Error. There your message would be given just before the code ends.....
    _....
    _.....
    _._____________________________
    I am not a Profi myself and prefer to have some test data ( reduced in size where possible ) so I can test and run any code I give. But for this error handler part I think I have done it often enough so I can work “blind” and it should work..
    -..

    So
    here we go
    _1 ) Change this bit of the code so that on encountering an Error, although an Exceptional State is still Erected, the code does not stop but is sent somewhere ( in this case to an error handling section towards the end of the code. ( The Error handling section can go anywhere, but as you want to stop shortly after going there it is convenient to put it there) ) :

    Please Login or Register  to view this content.
    _...........................


    _2) change the end of your code Thus:
    Please Login or Register  to view this content.
    _.. I could not test that, but it should do what you want

    _...........................
    _._________________________-

    As for the rest,
    Quote Originally Posted by BARENTINEMATT View Post
    ....
    I may need to post a new thread but additionally i would like to edit it somehow to copy the header of just the first workbook but not rest i initially though it would be pretty simple but i cant seem figure out a good starting piont.....
    .. it is probably Ok to post here as it fits the Thread Title, or you could start a new Thread, as you choose..
    As before , it is probably very easy to do. It is just a case of explaining carefully exactly what you want. Ideally some test data is needed, as the “Before”, and then some Hand filled in output as the “After”, showing exactly as you want the output to look based on your supplied sample input data.

    Alan

    P.s.
    Some notes here on how to get info across to us
    http://www.excelforum.com/hello-intr...ml#post4478582

    If you can show clearly what you want in a post in this thread ( the one we are in now ) , and if no one else does it for you I will do it for you tomorrow
    Last edited by Doc.AElstein; 10-01-2016 at 05:02 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Workbook.Open & Error Handling Best Practice
    By CraigsWorld in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-19-2015, 12:11 AM
  2. Workbook links, error handling, VBA
    By GeorgeMustang in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-19-2014, 07:07 AM
  3. Loop thru Charts in Workbook and Changing Chart 2/Error Handling
    By ker9 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2014, 10:16 AM
  4. [SOLVED] Open workbook from network location - error handling
    By supern0va in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-10-2012, 07:22 AM
  5. Handling Workbook Extensions
    By Ascension in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-29-2010, 12:47 PM
  6. Error Handling and linking VBA to specific workbook
    By Miraun in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-02-2009, 04:27 PM
  7. [SOLVED] Error handling with a handling routine
    By ben in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2005, 11:06 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1