+ Reply to Thread
Results 1 to 3 of 3

Thread: error handling filepath

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    77

    error handling filepath

    Hi,

    I am trying to create an error handling when I input a file path to copy from a sheet from another file and then paste into the current sheet. However I can't seem to get the if error get me to retry and ask me to reinput the file path again. The main file to extract is AY09Exam file whilst the Question_1data file is the retrieval file. thanks for your help in advance.


    
    Dim FP As String
    
    On Error GoTo myerrorline
        FP = InputBox("Enter File Path")
        Workbooks.Open Filename:=("FP")
    Exit Sub
    
    Worksheets("exp_data").Columns.Copy Destination:=Workbooks("Question2").Worksheets("DATA").Columns
    
    Workbooks("Question1_data").Close (SaveChanges = False)
    
    myerrorline:
            retry = MsgBox("Error, do you want to retry?", vbYesNo)
            If retry = vbYes Then Resume
            If retry = vbNo Then Exit Sub
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: error handling filepath

    I'd suggest handling the chance that the file is not a real file without explicitly using errors. The Dir() method can be used to check if a file exists. I'd also suggest using Excel's built in File Open Dialog box rather than an input box. I've included that in the code below, but commented it out; uncomment to use it.

    Public Sub kchm_2000()
    
        Dim strFile As String
        
        Do Until Dir(strFile) <> ""
            strFile = InputBox("Enter File Path")
            'strFile = Application.GetOpenFilename("Excel files (*.xls*), *.xls*")
        Loop
        
        Workbooks.Open Filename:=(strFile)
        Worksheets("exp_data").Columns.Copy Destination:=Workbooks("Question2").Worksheets("DATA").Columns
        Workbooks("Question1_data").Close (SaveChanges = False)
    
    End Sub
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: error handling filepath

    sub snb()
      do 
        fl=InputBox("Enter File Path")
      loop until fl="" or dir(fl)<>""
      if fl="" then exit sub
      
      With Workbooks.Open fl
         .sheets("exp_data").Columns.Copy ,Workbooks("Question2").sheets("DATA")
         .close 0
      end with
    
    End Sub



+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0