Reliability issue with some code

D

Dean

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top