BillDickenson
IS-IT--Management
I am updating an excel spreadheet from access. Before I export the query, I need to delete the worksheet thats already there. I seem to get sporadic responses to this code. Can someone point out what I'm missing ?
I'm getting a 438 error. Thanks
Function KillExcelSheet(szFileName As String, szSheetName As String) As Boolean
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim index As Integer
Dim szW1Name As String
Dim szW2Name As String
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If TypeName(xlApp) = "Nothing" Then
'Excel was not open
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlWB = xlApp.WorkBooks.Open(szFileName)
Call Bark("Killing " & szSheetName & " from" & szFileName, 2)
For index = 1 To xlWB.Worksheets.Count
szW1Name = Left(xlWB.Worksheets(index).Name, 3)
szW2Name = Left(szSheetName, 3)
'MsgBox szW1Name & ":" & szW2Name
'szW1Name = szW2Name
If szW1Name = szW2Name Then
MsgBox "Deleting " & xlWB.Worksheets(index).Name
'This line is generating a 438 error
xlWB.Worksheets(index).Remove
End If
Next index
With xlWB
.Save
.Close
End With
End Function
I'm getting a 438 error. Thanks
Function KillExcelSheet(szFileName As String, szSheetName As String) As Boolean
Dim xlApp As Object
Dim xlWB As Object
Dim xlWS As Object
Dim index As Integer
Dim szW1Name As String
Dim szW2Name As String
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If TypeName(xlApp) = "Nothing" Then
'Excel was not open
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
Set xlWB = xlApp.WorkBooks.Open(szFileName)
Call Bark("Killing " & szSheetName & " from" & szFileName, 2)
For index = 1 To xlWB.Worksheets.Count
szW1Name = Left(xlWB.Worksheets(index).Name, 3)
szW2Name = Left(szSheetName, 3)
'MsgBox szW1Name & ":" & szW2Name
'szW1Name = szW2Name
If szW1Name = szW2Name Then
MsgBox "Deleting " & xlWB.Worksheets(index).Name
'This line is generating a 438 error
xlWB.Worksheets(index).Remove
End If
Next index
With xlWB
.Save
.Close
End With
End Function