Hi,
I'm programming an application using Excel 2000. I have a macro inside this application that opens another workbook to extract data from it. Here is the code :
Sub openTrains()
Application.ScreenUpdating = False
Dim MySource As String
currentYear = Worksheets("Menu"
.Range("B35"
.Value
Mybook = ActiveWorkbook.Name
MySource = "C:\Prod\" & currentYear & "\" & "Daily Ore Train Movements" & ".xls"
On Error GoTo errorHandler
Workbooks.Open MySource
trainBook = ActiveWorkbook.Name
With Worksheets("Billing"
traindata(0) = .Range("E64"
.Value
traindata(1) = .Range("I70"
.Value
End With
Exit Sub
errorHandler:
If Err.Number = 1004 Then
MsgBox ("Error Openning the file"
End If
Resume
End Sub
If an error is capted it means that the file was not found or that the file is already opened. I want to know how to tell the machine that if the file is already opened to extract the data from the opened file.
Another thing that you should notice is that after openning the file I put the name of the opened workbook inside trainBook to be able to close it afterwords. This works fine if the file opens without any errors but when there is an error I capt the name of the active workbook and close it afterwords. This is not good.
Any suggestions or insights on error capting would be greatly appreciated. Thx
I'm programming an application using Excel 2000. I have a macro inside this application that opens another workbook to extract data from it. Here is the code :
Sub openTrains()
Application.ScreenUpdating = False
Dim MySource As String
currentYear = Worksheets("Menu"
Mybook = ActiveWorkbook.Name
MySource = "C:\Prod\" & currentYear & "\" & "Daily Ore Train Movements" & ".xls"
On Error GoTo errorHandler
Workbooks.Open MySource
trainBook = ActiveWorkbook.Name
With Worksheets("Billing"
traindata(0) = .Range("E64"
traindata(1) = .Range("I70"
End With
Exit Sub
errorHandler:
If Err.Number = 1004 Then
MsgBox ("Error Openning the file"
End If
Resume
End Sub
If an error is capted it means that the file was not found or that the file is already opened. I want to know how to tell the machine that if the file is already opened to extract the data from the opened file.
Another thing that you should notice is that after openning the file I put the name of the opened workbook inside trainBook to be able to close it afterwords. This works fine if the file opens without any errors but when there is an error I capt the name of the active workbook and close it afterwords. This is not good.
Any suggestions or insights on error capting would be greatly appreciated. Thx