P
Pacers02
Hello. I am having some problems with some excel vba code I am running from
access. The code will work sometimes and will fail sometimes under the exact
same conditions. I have been unable to identify the root cause, however it
is failing at the section where the sheets are deleted. Any help would be
greatly appreciated.
Code:
Sub TestFileCopy()
On Error GoTo ErrorHandler
If msgBox("Would you like to import and prepare prior weeks BIC file?",
vbYesNo, "Import File") = vbYes Then
Dim bicPath As String
Dim fileName As String
Dim savePath As String
Dim dt As String
Dim fs As FileSystemObject
Dim xl As Object
'Set variables
dt = "1-28-2007"
bicPath = "PathName"
fileName = "File Name"
savePath = "PathName"
'Copy over the file
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile bicPath & fileName, savePath
'Open file and prepare it for importing
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open savePath & fileName
' xl.Visible = True
xl.DisplayAlerts = False
'Determines if this is a shared file. If it is, it converts to a
non-shared file
If xl.Workbooks(fileName).MultiUserEditing Then
xl.Workbooks(fileName).ExclusiveAccess
End If
xl.Workbooks(fileName).Close
' close wb and reopen for debugging
xl.Workbooks.Open savePath & fileName
xl.Visible = True
'If sheet name doesn't = ESN, ANIS, STAT, MEID then it is deleted
xl.Workbooks(fileName).Activate
Dim i As Integer
i = 1
Do
ActiveWorkbook.Worksheets(i).Activate
'ActiveWorkbook.Worksheets(i).Activate
'ActiveSheet.Visible = True
'This section only updates i to the next counter number if the sheet
is not deleted, otherwise it skips the next sheet
If ActiveWorkbook.ActiveSheet.Name <> "ANIS" And ActiveSheet.Name <>
"ESN" And ActiveSheet.Name <> "STAT" And ActiveSheet.Name <> "MEID" Then
ActiveWorkbook.ActiveSheet.Delete
Else
i = i + 1
End If
Loop While i < (Worksheets.Count + 1)
xl.Workbooks(fileName).Save
xl.Workbooks(fileName).Close
xl.DisplayAlerts = True
End If
msgBox "Done"
Exit Sub
ErrorHandler:
msgBox "Error number: " & Err.Number & Chr(10) & Err.Description
End Sub
access. The code will work sometimes and will fail sometimes under the exact
same conditions. I have been unable to identify the root cause, however it
is failing at the section where the sheets are deleted. Any help would be
greatly appreciated.
Code:
Sub TestFileCopy()
On Error GoTo ErrorHandler
If msgBox("Would you like to import and prepare prior weeks BIC file?",
vbYesNo, "Import File") = vbYes Then
Dim bicPath As String
Dim fileName As String
Dim savePath As String
Dim dt As String
Dim fs As FileSystemObject
Dim xl As Object
'Set variables
dt = "1-28-2007"
bicPath = "PathName"
fileName = "File Name"
savePath = "PathName"
'Copy over the file
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile bicPath & fileName, savePath
'Open file and prepare it for importing
Set xl = CreateObject("Excel.Application")
xl.Workbooks.Open savePath & fileName
' xl.Visible = True
xl.DisplayAlerts = False
'Determines if this is a shared file. If it is, it converts to a
non-shared file
If xl.Workbooks(fileName).MultiUserEditing Then
xl.Workbooks(fileName).ExclusiveAccess
End If
xl.Workbooks(fileName).Close
' close wb and reopen for debugging
xl.Workbooks.Open savePath & fileName
xl.Visible = True
'If sheet name doesn't = ESN, ANIS, STAT, MEID then it is deleted
xl.Workbooks(fileName).Activate
Dim i As Integer
i = 1
Do
ActiveWorkbook.Worksheets(i).Activate
'ActiveWorkbook.Worksheets(i).Activate
'ActiveSheet.Visible = True
'This section only updates i to the next counter number if the sheet
is not deleted, otherwise it skips the next sheet
If ActiveWorkbook.ActiveSheet.Name <> "ANIS" And ActiveSheet.Name <>
"ESN" And ActiveSheet.Name <> "STAT" And ActiveSheet.Name <> "MEID" Then
ActiveWorkbook.ActiveSheet.Delete
Else
i = i + 1
End If
Loop While i < (Worksheets.Count + 1)
xl.Workbooks(fileName).Save
xl.Workbooks(fileName).Close
xl.DisplayAlerts = True
End If
msgBox "Done"
Exit Sub
ErrorHandler:
msgBox "Error number: " & Err.Number & Chr(10) & Err.Description
End Sub