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
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
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks