S
Simon White
Hi,
I have some very simple VBA6 code, which gives an error
when run. But when I step through it, it runs fine
without giving the error!
More specifically, the code is trying to un-protect a
worksheet, then refresh a DataQuery on the worksheet, then
protect the worksheet again.
The code is:
------------------------------------------------
Public Sub refreshAll()
Dim timeSheet As Worksheet
Dim protectionDrawingObjectsState As Boolean
Dim protectionContentsState As Boolean
Dim protectionScenariosState As Boolean
Dim protectionUIState As Boolean
On Error GoTo ErrHandler
// Break here
Set timeSheet = ActiveWorkbook.Sheets("Timesheet")
' Temporarily unprotect the time sheet
protectionDrawingObjectsState =
timeSheet.ProtectDrawingObjects
protectionContentsState = timeSheet.ProtectContents
protectionScenariosState = timeSheet.ProtectScenarios
protectionUIState = timeSheet.ProtectionMode
timeSheet.Unprotect (PROTECTION_PASSWORD)
' Refresh all the query tables
ActiveWorkbook.refreshAll
' Restore protection and exit
GoSub RestoreProtection
Exit Sub
RestoreProtection:
Call timeSheet.Protect(PROTECTION_PASSWORD,
protectionDrawingObjectsState, protectionContentsState,
protectionScenariosState, protectionUIState)
Return
ErrHandler:
GoSub RestoreProtection
MsgBox Err.Description
Exit Sub
End Sub
------------------------------------------------
There is only one data query in the workbook, and it is on
the Timesheet sheet.
If I break at the top, and then step through it (with
Shift F8), it works fine - the data query gets refreshed,
and the sheet returns to its protected state.
If I break at the top, then run (with F5), it gives the
following error:
"The cell or chart you are trying to change is protected
and therefore read-only. To modify a protected cell or
chart, first remove the protection ... etc."
Please help - I don't understand why, if there is a
problem, I don't get the error while stepping through.
Thanks for any advice,
Simon.
I have some very simple VBA6 code, which gives an error
when run. But when I step through it, it runs fine
without giving the error!
More specifically, the code is trying to un-protect a
worksheet, then refresh a DataQuery on the worksheet, then
protect the worksheet again.
The code is:
------------------------------------------------
Public Sub refreshAll()
Dim timeSheet As Worksheet
Dim protectionDrawingObjectsState As Boolean
Dim protectionContentsState As Boolean
Dim protectionScenariosState As Boolean
Dim protectionUIState As Boolean
On Error GoTo ErrHandler
// Break here
Set timeSheet = ActiveWorkbook.Sheets("Timesheet")
' Temporarily unprotect the time sheet
protectionDrawingObjectsState =
timeSheet.ProtectDrawingObjects
protectionContentsState = timeSheet.ProtectContents
protectionScenariosState = timeSheet.ProtectScenarios
protectionUIState = timeSheet.ProtectionMode
timeSheet.Unprotect (PROTECTION_PASSWORD)
' Refresh all the query tables
ActiveWorkbook.refreshAll
' Restore protection and exit
GoSub RestoreProtection
Exit Sub
RestoreProtection:
Call timeSheet.Protect(PROTECTION_PASSWORD,
protectionDrawingObjectsState, protectionContentsState,
protectionScenariosState, protectionUIState)
Return
ErrHandler:
GoSub RestoreProtection
MsgBox Err.Description
Exit Sub
End Sub
------------------------------------------------
There is only one data query in the workbook, and it is on
the Timesheet sheet.
If I break at the top, and then step through it (with
Shift F8), it works fine - the data query gets refreshed,
and the sheet returns to its protected state.
If I break at the top, then run (with F5), it gives the
following error:
"The cell or chart you are trying to change is protected
and therefore read-only. To modify a protected cell or
chart, first remove the protection ... etc."
Please help - I don't understand why, if there is a
problem, I don't get the error while stepping through.
Thanks for any advice,
Simon.