Hi
I'm having some trouble with the following. I run a macro which opens a file makes some changes and then closes the file saving the changes.
After the close excel throws an error 5.
can anyone throw some light on why this is happening.
Using Excel 2002
Sub testwb()
Dim strBOLogFile As String
Dim wbHistory As Workbook
lngToday = 40751
strBOLogFile = "file.xls"
g_pswdBOLog = "pw"
lret = 1
If lret = 1 Then
'Open Bid Offer History Log
Set wbHistory = Workbooks.Open(strBOLogFile, , , , , g_pswdBOLog, True)
Set wsHistory = wbHistory.Worksheets("BidOfferHistory")
'Update BO History record
i = 1
Do Until wsHistory.Range("BOHistory").Offset(i, 0) = "" Or wsHistory.Range("BOHistory").Offset(i, 0) = lngToday
i = i + 1
Loop
i = 1500
If wsHistory.Range("BOHistory").Offset(i, 0) = lngToday And Not SPH_QUIET_MODE Then
lngRet = MsgBox("History already exists for " & CDate(lngToday) & vbCrLf & "Do you want to overwrite?", vbYesNo, "History Exists")
Else
lngRet = vbYes
End If
If lngRet = vbYes Then
With wsHistory
.Range("BOHistory").Offset(i, 0).Value = lngToday
.Range("BOHistory").Offset(i, 0).NumberFormat = "dd-MMM-yy"
.Range("BOHistory").Offset(i, 1).Value = dblBidOffer * 0.01 'Bid offer on standard trades (£)
.Range("BOHistory").Offset(i, 2).Value = dblQuoteBO * 0.01 'Bid offer on special quoted prices (£)
End With
End If
If Not Err.Number = 0 Then
strErrorMsg = "Error " & Err.Number & vbCrLf & _
"Source " & IIf(Err.Source = g_VBAProjectName, strFnName, Err.Source) & vbCrLf & _
"Message " & Err.Description
MsgBox strErrorMsg 'Display error if debug is on
Err.Clear
End If
wbHistory.Save
' NO ERROR AT THIS STAGE, WB SAVES OK
If Not Err.Number = 0 Then
strErrorMsg = "Error " & Err.Number & vbCrLf & _
"Source " & IIf(Err.Source = g_VBAProjectName, strFnName, Err.Source) & vbCrLf & _
"Message " & Err.Description
MsgBox strErrorMsg 'Display error if debug is on
Err.Clear
End If
wsHistory.Range("A1").Select
Workbooks(wbHistory.Name).Close SaveChanges:=False
' ERROR THROWN HERE AFTER CLOSE
If Not Err.Number = 0 Then
strErrorMsg = "Error " & Err.Number & vbCrLf & _
"Source " & IIf(Err.Source = g_VBAProjectName, strFnName, Err.Source) & vbCrLf & _
"Message " & Err.Description
MsgBox strErrorMsg 'Display error if debug is on
Err.Clear
End If
End If
End Sub
I'm having some trouble with the following. I run a macro which opens a file makes some changes and then closes the file saving the changes.
After the close excel throws an error 5.
can anyone throw some light on why this is happening.
Using Excel 2002
Sub testwb()
Dim strBOLogFile As String
Dim wbHistory As Workbook
lngToday = 40751
strBOLogFile = "file.xls"
g_pswdBOLog = "pw"
lret = 1
If lret = 1 Then
'Open Bid Offer History Log
Set wbHistory = Workbooks.Open(strBOLogFile, , , , , g_pswdBOLog, True)
Set wsHistory = wbHistory.Worksheets("BidOfferHistory")
'Update BO History record
i = 1
Do Until wsHistory.Range("BOHistory").Offset(i, 0) = "" Or wsHistory.Range("BOHistory").Offset(i, 0) = lngToday
i = i + 1
Loop
i = 1500
If wsHistory.Range("BOHistory").Offset(i, 0) = lngToday And Not SPH_QUIET_MODE Then
lngRet = MsgBox("History already exists for " & CDate(lngToday) & vbCrLf & "Do you want to overwrite?", vbYesNo, "History Exists")
Else
lngRet = vbYes
End If
If lngRet = vbYes Then
With wsHistory
.Range("BOHistory").Offset(i, 0).Value = lngToday
.Range("BOHistory").Offset(i, 0).NumberFormat = "dd-MMM-yy"
.Range("BOHistory").Offset(i, 1).Value = dblBidOffer * 0.01 'Bid offer on standard trades (£)
.Range("BOHistory").Offset(i, 2).Value = dblQuoteBO * 0.01 'Bid offer on special quoted prices (£)
End With
End If
If Not Err.Number = 0 Then
strErrorMsg = "Error " & Err.Number & vbCrLf & _
"Source " & IIf(Err.Source = g_VBAProjectName, strFnName, Err.Source) & vbCrLf & _
"Message " & Err.Description
MsgBox strErrorMsg 'Display error if debug is on
Err.Clear
End If
wbHistory.Save
' NO ERROR AT THIS STAGE, WB SAVES OK
If Not Err.Number = 0 Then
strErrorMsg = "Error " & Err.Number & vbCrLf & _
"Source " & IIf(Err.Source = g_VBAProjectName, strFnName, Err.Source) & vbCrLf & _
"Message " & Err.Description
MsgBox strErrorMsg 'Display error if debug is on
Err.Clear
End If
wsHistory.Range("A1").Select
Workbooks(wbHistory.Name).Close SaveChanges:=False
' ERROR THROWN HERE AFTER CLOSE
If Not Err.Number = 0 Then
strErrorMsg = "Error " & Err.Number & vbCrLf & _
"Source " & IIf(Err.Source = g_VBAProjectName, strFnName, Err.Source) & vbCrLf & _
"Message " & Err.Description
MsgBox strErrorMsg 'Display error if debug is on
Err.Clear
End If
End If
End Sub