ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   error trapping (https://www.excelbanter.com/excel-discussion-misc-queries/83082-error-trapping.html)

flow23

error trapping
 
I have the following macro. which works fine when the user type the password
correctly.

however if the user types the password incorrectly. it takes them to the VBA
code.

Is there a way around this?
sub unprotect()
' Application.CutCopyMode = False
Sheets("update").Visible = True
Sheets("update").Select
ActiveSheet.unprotect
Sheets("update").Select

ActiveSheet.Shapes("Label1").Visible = True
ActiveSheet.Shapes("Label2").Visible = True
ActiveSheet.Shapes("Label3").Visible = True
ActiveSheet.Shapes("Label4").Visible = True
ActiveSheet.Shapes("Label5").Visible = True
ActiveSheet.Shapes("Label6").Visible = True
ActiveSheet.Shapes("Label7").Visible = True

Sheets("update").Cells.Select
Selection.EntireColumn.Hidden = False
Sheets("fields").Visible = True
end sub


Dave Peterson

error trapping
 
Maybe something like:

Option Explicit
Sub unprotect()
' Application.CutCopyMode = False

With Sheets("Update")
On Error Resume Next
.unprotect
On Error GoTo 0
If .ProtectContents _
Or .ProtectDrawingObjects _
Or .ProtectScenarios Then
'it's still protected
MsgBox "Nope!"
Else
.Visible = xlSheetVisible
.Shapes("Label1").Visible = True
.Shapes("Label2").Visible = True
.Shapes("Label3").Visible = True
.Shapes("Label4").Visible = True
.Shapes("Label5").Visible = True
.Shapes("Label6").Visible = True
.Shapes("Label7").Visible = True
.Cells.EntireColumn.Hidden = False

Sheets("fields").Visible = True
End If
End With
End Sub

flow23 wrote:

I have the following macro. which works fine when the user type the password
correctly.

however if the user types the password incorrectly. it takes them to the VBA
code.

Is there a way around this?
sub unprotect()
' Application.CutCopyMode = False
Sheets("update").Visible = True
Sheets("update").Select
ActiveSheet.unprotect
Sheets("update").Select

ActiveSheet.Shapes("Label1").Visible = True
ActiveSheet.Shapes("Label2").Visible = True
ActiveSheet.Shapes("Label3").Visible = True
ActiveSheet.Shapes("Label4").Visible = True
ActiveSheet.Shapes("Label5").Visible = True
ActiveSheet.Shapes("Label6").Visible = True
ActiveSheet.Shapes("Label7").Visible = True

Sheets("update").Cells.Select
Selection.EntireColumn.Hidden = False
Sheets("fields").Visible = True
end sub


--

Dave Peterson

Dave Peterson

error trapping
 
PS. I wouldn't use Unprotect as the name of the subroutine, either. It may not
confuse excel/vba, but it could confuse me!

Dave Peterson wrote:

Maybe something like:

Option Explicit
Sub unprotect()
' Application.CutCopyMode = False

With Sheets("Update")
On Error Resume Next
.unprotect
On Error GoTo 0
If .ProtectContents _
Or .ProtectDrawingObjects _
Or .ProtectScenarios Then
'it's still protected
MsgBox "Nope!"
Else
.Visible = xlSheetVisible
.Shapes("Label1").Visible = True
.Shapes("Label2").Visible = True
.Shapes("Label3").Visible = True
.Shapes("Label4").Visible = True
.Shapes("Label5").Visible = True
.Shapes("Label6").Visible = True
.Shapes("Label7").Visible = True
.Cells.EntireColumn.Hidden = False

Sheets("fields").Visible = True
End If
End With
End Sub

flow23 wrote:

I have the following macro. which works fine when the user type the password
correctly.

however if the user types the password incorrectly. it takes them to the VBA
code.

Is there a way around this?
sub unprotect()
' Application.CutCopyMode = False
Sheets("update").Visible = True
Sheets("update").Select
ActiveSheet.unprotect
Sheets("update").Select

ActiveSheet.Shapes("Label1").Visible = True
ActiveSheet.Shapes("Label2").Visible = True
ActiveSheet.Shapes("Label3").Visible = True
ActiveSheet.Shapes("Label4").Visible = True
ActiveSheet.Shapes("Label5").Visible = True
ActiveSheet.Shapes("Label6").Visible = True
ActiveSheet.Shapes("Label7").Visible = True

Sheets("update").Cells.Select
Selection.EntireColumn.Hidden = False
Sheets("fields").Visible = True
end sub


--

Dave Peterson


--

Dave Peterson

flow23

error trapping
 

Thanks

that works fab

Re. the name.. IT was only for demo. the macro is linked to a command button


"Dave Peterson" wrote:

PS. I wouldn't use Unprotect as the name of the subroutine, either. It may not
confuse excel/vba, but it could confuse me!

Dave Peterson wrote:

Maybe something like:

Option Explicit
Sub unprotect()
' Application.CutCopyMode = False

With Sheets("Update")
On Error Resume Next
.unprotect
On Error GoTo 0
If .ProtectContents _
Or .ProtectDrawingObjects _
Or .ProtectScenarios Then
'it's still protected
MsgBox "Nope!"
Else
.Visible = xlSheetVisible
.Shapes("Label1").Visible = True
.Shapes("Label2").Visible = True
.Shapes("Label3").Visible = True
.Shapes("Label4").Visible = True
.Shapes("Label5").Visible = True
.Shapes("Label6").Visible = True
.Shapes("Label7").Visible = True
.Cells.EntireColumn.Hidden = False

Sheets("fields").Visible = True
End If
End With
End Sub

flow23 wrote:

I have the following macro. which works fine when the user type the password
correctly.

however if the user types the password incorrectly. it takes them to the VBA
code.

Is there a way around this?
sub unprotect()
' Application.CutCopyMode = False
Sheets("update").Visible = True
Sheets("update").Select
ActiveSheet.unprotect
Sheets("update").Select

ActiveSheet.Shapes("Label1").Visible = True
ActiveSheet.Shapes("Label2").Visible = True
ActiveSheet.Shapes("Label3").Visible = True
ActiveSheet.Shapes("Label4").Visible = True
ActiveSheet.Shapes("Label5").Visible = True
ActiveSheet.Shapes("Label6").Visible = True
ActiveSheet.Shapes("Label7").Visible = True

Sheets("update").Cells.Select
Selection.EntireColumn.Hidden = False
Sheets("fields").Visible = True
end sub


--

Dave Peterson


--

Dave Peterson



All times are GMT +1. The time now is 12:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com