+ Reply to Thread
Results 1 to 6 of 6

Error Trapping When Reading From Another Workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    12-02-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Error Trapping When Reading From Another Workbook

    Hello EF. I'm using VBA to open several files and pull information from them into a workbook with the hopes of eventually outputting the information to a formatted text file that can be uploaded to a data transfer system that will fill out a form for me. The problem I am finding is that the workbook that I need to access has faulty VBA code that executes everytime the workbook is opened. I realize the easiest solution would be to fix the code that is faulty however the workbook that I am accessing is automatically generated for each product with the faulty code embedded requiring me to handle the error on the back end. Attached is my code and I attempted using error trapping to handle this issue but I've concluded that I either lack understanding of error trapping or the fact that the error is in a cascaded workbook complicates the error trapping code. More than likely the former. Thank you in advance for your insight.

    Book1.xlsm

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error Trapping When Reading From Another Workbook

    Hie and welcome to the forum.

    Try this (not tested).

    Sub PullABHData()
        Dim wb As Workbook
        ' turn off the screen updating
        Application.ScreenUpdating = False
        
        On Error Resume Next
        Application.EnableEvents = False    'Disable event processing (doesn't trigger the Workbook_Open macro.)
        
        ' open the source workbook, read only
        Set wb = Workbooks.Open("J:\Active\" & JobNumber & "\Drafting\Headers\" & JobNumber & "-1_ABH_REV0.xls", True, True)
        
        ' read data from the source workbook
        
        With ThisWorkbook.Worksheets("Sheet1")
            
            .Range("A1").Formula = wb.Worksheets("A").Range("M26").Formula
            .Range("A2").Formula = wb.Worksheets("A").Range("M25").Formula
            .Range("A3").Formula = wb.Worksheets("A").Range("G54").Formula
            .Range("A4").Formula = wb.Worksheets("A").Range("M23").Formula
            .Range("A5").Formula = wb.Worksheets("A").Range("M27").Formula
            .Range("A6").Formula = wb.Worksheets("A").Range("G54").Formula
            .Range("A7").Formula = wb.Worksheets("A").Range("M23").Formula
        End With
        ' close the source workbook without saving any changes
        wb.Close False
        ' free memory
        Set wb = Nothing
        
        Application.EnableEvents = True
        On Error GoTo 0
        
        ' turn on the screen updating
        Application.ScreenUpdating = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    12-02-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Error Trapping When Reading From Another Workbook

    That worked beautifully! Thank you very much.

  4. #4
    Registered User
    Join Date
    12-02-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Error Trapping When Reading From Another Workbook

    Would any changes within the workbook being opened be able break this form of error trapping? All of a sudden this doesn't seem to be working.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Error Trapping When Reading From Another Workbook

    Quote Originally Posted by redtop91 View Post
    Would any changes within the workbook being opened be able break this form of error trapping? All of a sudden this doesn't seem to be working.
    Care to elaborate on what "doesn't seem to be working" exactly means; nothing happens, and error is thrown (and if so what error, description, and line), wrong data copied?

    Does the workbook being opened always have a worksheet named "A"?

  6. #6
    Registered User
    Join Date
    12-02-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Error Trapping When Reading From Another Workbook

    I apologize that was an uncharacteristically vague questioin on my part. The workbook that is being opened and read from throws the original error that was encountered "Compile Error: Can't Find Project or Library" Essentially with no changes in my workbook the error trapping is no longer trapping the same error that it was trapping before. The workbook being opened always has a sheet named A yes however I suppose referencing the sheet number might be more robust.

    Edit: A network wide upgrade from Excel 2007 to 2010 may be the culprit?
    Last edited by redtop91; 08-06-2014 at 03:14 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. Trapping an error
    By deucejmp in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2011, 06:31 PM
  2. Trapping error in add-in
    By webbug08 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-05-2009, 05:04 AM
  3. Error Trapping
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-07-2007, 11:47 AM
  4. error trapping
    By JohnE in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-27-2006, 10:55 AM
  5. [SOLVED] Error Trapping
    By Adrian in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-31-2005, 03:06 PM

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