I am new in using Excel VBA. I have an Excel VBA code that will duplicate a certain sheet for n times. I want to prevent user from changing the workbook structure manually (such as adding new sheet, duplicating sheet, or moving sheet), but I still want the VBA to be able to run.
How should I do it?
I was hoping you could use UserInterfaceOnly but that appears to be only worksheet level and not workbook level - it locks the worksheet for users, but allows VBA code to make updates.
The only other way I can think of is to lock and unlock the workbook as needed:
Sub Test()
Dim wrkSht As Worksheet
UnlockWorkbook
Set wrkSht = ThisWorkbook.Worksheets.Add
LockWorkbook
End Sub
Sub LockWorkbook()
ThisWorkbook.Protect Password:="aaa", Structure:=True
End Sub
Sub UnlockWorkbook()
ThisWorkbook.Unprotect Password:="aaa"
End Sub
Related
I have a workbook consisting of 44 sheets. In this workbook, I have a VBA code that protects and unprotects the sheets, hides and unhides sheets, inserts and deletes inserted sheets.
How do I protect the workbook such that a user doesn't delete, rename or even move the sheets?
I tried the protect workbook structure but it prevents insertion of sheets. How do I go about this?
One thing you could do is rename and move the Sheet any time it is activated. Each worksheet has a worksheet name(Sheet1) and a Caption("MyWorksheet"). If the user changes the caption, you will not be able to reference the caption, so reference the sheet name instead. You can set a sheet name or just reference the default sheet name. In the VBA IDE, in the VBAProject open the Micorsoft Excel Objects folder in the Project window, and open the sheet instead of the Modules. In the dropdown at the top select Worksheet, then in the second drop down on the right select Activate. This will generate the following code, or you could just copy paste the following into the page. I added the renaming of the sheet caption and the move sheet code.
Option Explicit
Private Sub Worksheet_Activate()
On Error Resume Next
Sheet5.Name = "TEST"
Sheet5.Move After:=Sheet1
On Error GoTo 0
End Sub
Now anytime someone clicks the sheet, the sheet will be renamed to its original name and moved to where you want it in the workbook.
If you want to still protect the workbook, you can do this. You would have to add a button in the ribbon, worksheet, or a keyboard shortcut for the user to run it.
Sub newSheet()
Dim sheetName As Variant
sheetName = InputBox("What do you want to name the sheet?")
ActiveWorkbook.Unprotect 'password if needed
ActiveWorkbook.Sheets.Add.Name = sheetName
ActiveWorkbook.Protect Structure:=True, Windows:=False
End Sub
Trying to create a workbook that gets data from ACCESS. I can open the workbook and was adding some code to auto open in excel. But when i tried to edit it keeps telling me that I must unhide hidden work sheet. And the unhide command is grayed out. When I first set it up I selected Personal Workbook, which I think applies to any book I open. Along the way I kept deleting workbooks in order start over, so I think there is nothing really to delete. I wanted to add this code to to auto start but I can't get to the code. The follwing code is supposed to unhide all hidden workboks/sheets:
Sub Viewit()
Dim Ws As Worksheet
Application.ScreenUpdating = False
For Each Ws In Worksheets
Ws.Visible = True
Next Ws
Application.ScreenUpdating = True
End Sub
Looking for a better solution and what I am doing that is wrong.
Thanks
There is a very hidden level :
ActiveWorkbook.Sheets("sheet name").Visible = xlSheetVeryHidden
Or xlSheetVeryVisible for the opposite effect
Note this level is only controllable through vba, but functions can work with cells on very hidden sheets.
I am able to disable the display of page breaks for the single worksheet in Excel,but i want to disable for the whole workbook at a time .Please someone help me to disable the display of pagebreaks for the workbook by using VBA.
This has already been answered, but here's what you need:
Sub DisablePagebreak()
Dim ws As Worksheet
For Each ws In Worksheets
ws.DisplayPageBreaks = False
Next
End Sub
Worksheets is the collection of all Worksheets from the current Workbook. As the name's implying .DisplayPageBreaks hides/shows the page breaks.
My Excel workbook has 4 sheets but the VBA editor shows 5 sheets in same workbook.
How do I make all sheets visible?
Edited Answer. See comments from Jeeped and Gserg(at original question)
Maybe the missing worksheet is very hidden.
Try this code to unhide it.
Sub test()
Dim ws As Worksheet
For Each ws In Sheets
ws.Visible = True
Next
End Sub
Changing the visible property in the properties-window also works.
I have a spreadsheet with several sheets, which is protected except where I want people to make changes, and all is password protected. I am trying to make a command button so others can view the data, but can't make changes to the cells. Here is what I have (not working quite right).
Private Sub mdRead_Click()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
'To open wookbook as read only, while proctecting changes.
Worksheet.Unprotect = True
Worksheet.Range("C10:I23,L10:R23,C25:I36,L25:R36,C45:I58,L45:I58,C60:I71,L60:R71").Select
Selection.Locked = True
Next ws
End Sub
Having set the Locked property, you might want to protect the sheet again.