Prevent user from Deleting and Re-Arranging Worksheets using Excel VBA 2013

Anonymous
2014-02-04T17:56:03+00:00

Hi,

Is there a way in VBA to prevent the user from deliberately deleting or re-arranging worksheets using Excel VBA 2013 ? Obviously, I need to prevent users from trying to mess up with the application.

Can someone please help. I have found several ways by searching for these topics, suggesting several different approaches, all sound confusing and hard to understand. Please help!

Thank you.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2014-02-05T07:07:22+00:00

The code I supplied was not intended to go into a ThisWorkbook sheet, just a standard Module sheet as you've discovered.

When I referred to ThisWorkbook, I was referring to the code that read,

With ThisWorkbook

...

End With

, which might have to be changed to,

With ActiveWorkbook

...

End With

, or...

Set wb = Workbooks.open(<some workbook>)

With wb

...

End with

Was this answer helpful?

0 comments No comments
Answer accepted by question author
Anonymous
2014-02-05T01:55:02+00:00

These assume that the workbook has already been protected (structure only) with the password my_p_word.

Option Explicit

Public Const csPASSWORD As String = "my_p_word"

Sub Add_a_Worksheet()

With ThisWorkbook

.Unprotect Password:=csPASSWORD

.Sheets.Add After:=.Sheets(.Sheets.Count)

.Protect Structure:=True, Windows:=False, Password:=csPASSWORD

End With

End Sub

Sub Leave_Single_Worksheet()

With ThisWorkbook

.Unprotect Password:=csPASSWORD

Do While .Sheets.Count > 1

Application.DisplayAlerts = False

.Sheets(2).Delete

Application.DisplayAlerts = True

Loop

.Protect Structure:=True, Windows:=False, Password:=csPASSWORD

End With

End Sub

I've put the password into a single global variable so you can use it in multiple places but only have to edit it in one place if you change it further down the road. The code uses ThisWorkbook (e.g. the one holding the code module). If you want to work on another workbook you will have to change that to the workbook handle that was opened or perhaps ActiveWorkbook.

Was this answer helpful?

0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2014-02-05T01:25:57+00:00

    You will want to protect the workbook structure. If you have to manipulate the structure within VBA, un-protect it with VBA, modify it and then re-protect it.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-02-05T01:16:01+00:00

    Hi Jim,

    You suggest protecting the whole workbook, but the workbook I am working on is macro enabled and thus some of the macros do not work properly under workbook protection. Is'nt there any other way through code to do this ?

    Appreciate your help.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2014-02-04T18:42:51+00:00

    Re: prevent sheets from being moved or deleted

    Protect the workbook.

    '---

    Jim Cone

    Portland, Oregon USA

    https://goo.gl/IUQUN2 (Dropbox)

    (free & commercial excel add-ins & workbooks)

    Was this answer helpful?

    0 comments No comments