ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reliability issue with some code (https://www.excelbanter.com/excel-programming/357094-reliability-issue-some-code.html)

Dean[_9_]

Reliability issue with some code
 
Hi, I am having some reliability issues with this code. Sometimes it
works other times it will produce a "runtime error 9" highlighting this
line of code:

Set wbTarget = Workbooks(NameOfFile)

My VBA skills are a little on the basic side at best and would
appreciate any advice on to eradicate this error once and for all.

Regards,
Dean

Sub Macro1()
'Macro purpose: Use the application.run method to execute
'a macro without arguments from another workbook

Dim PathToFile As String, _
NameOfFile As String, _
wbTarget As Workbook, _
CloseIt As Boolean

ActiveSheet.Unprotect Password:="XXXXXX"

Application.ScreenUpdating = False
MsgBox "This will take around 30 Minutes to Process"


'Set file name and location.
NameOfFile = "data.xls"
PathToFile = "C:\winnt40\profiles\backoffice\desktop\label update"

'Attempt to set the target workbook to a variable. If an error is
'generated, then the workbook is not open, so open it
On Error Resume Next
Set wbTarget = Workbooks(NameOfFile)

If Err.Number < 0 Then
'Open the workbook
Err.Clear
Set wbTarget = Workbooks.Open(PathToFile & "\" & NameOfFile)
CloseIt = True
End If

'Check and make sure workbook was opened
If Err.Number = 1004 Then
MsgBox "Sorry, but the file you specified does not exist!" _
& vbNewLine & PathToFile & "\" & NameOfFile
Exit Sub
End If
On Error GoTo 0

'Run the macro!
Application.Run Macro:="process_update"


End Sub



All times are GMT +1. The time now is 12:32 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com