Locking specific cells and creating a summary sheet - excel

I have a workbook I'm trying to create a vba code for but I keep running into different errors...runtimee error, object error etc
I have created a series of forms and I need to lock the cells that contain specific texts. The forms ask a series of questions, including First name, Last name and Degree. I need to lock the cells containing these texts but leave those that will contain the responses open.
Also, I need the responses for the cells containing Degrees and location to feed a summary sheet.
The closest I got was using an If function but it didn't quite cover everything I wanted to do. Please help if you can.

If you are using User Forms to collect the data then you are likely using VBA to transfer the user form data to te worksheet. If the worksheet is protected with the UserInterfaceOnly:=true argument, you can do anything you want with the worksheet's data (e.g. add/revise/delete/etc) through VBA and the user cannot make any modifications.
Run this once.
sub allowVBA()
with thisworkbook.worksheets("sheet1")
.unprotect password:="password"
.protect password:="password", userinterfaceonly:=true
end with
end sub

Related

Hide or unhide Excel sheet but formulas should work

I have used this method before but after six months I cannot.
I created data entry fields through which I move data from Sheet1 to Sheet2 using a button.
I do not want users to see Sheet2 so that they cannot change anything. I was using the formulas below.
Sheets("Data Sheet").Visible = True
Sheets("Data Sheet").Visible = False
After hiding the sheet I protect them with the password. The main advantage of code is that I can still post the data from Sheet1 to Sheet2 after hiding.
Upon research I found one more formula to hide the sheet but after hiding sheet2 posting via Sheet1 to Sheet2 (which is hidden) gives an error.
ThisWorkbook.Worksheets(Array("Data Sheet")).Visible = xlSheetHidden
How can I hide Sheet2 and continue posting the entries via Sheet1.
You can manipulate data on a hidden sheet, no need to unhide it.
Unless you are trying to select cells, but you shouldn't have to do that.
You do however need to unlock a sheet before editing it if the cells are protected. You can do this as part of the process.
Sheets("Data Sheet").Unprotect Password:="pass"
----
whatever code you have
----
Sheets("Data Sheet").Protect Password:="pass"
You do might want to have some error handling in there however, since if the code throws an error, the sheet will be left unlocked.
a) The statements Sheets("Data Sheet").Visible = False and ThisWorkbook.Worksheets(Array("Data Sheet")).Visible = xlSheetHidden do basically the same. However, you will need to understand why it is important to qualify the Sheets: ThisWorkbook.Worksheets will access the worksheets from the workbook where the code lives in, just writing Sheets (or Worksheets) will access the sheets of the ActiveWorkbook - and that may be a different workbook. The Array("Data Sheet")-part is unnecessary in your case (you can pass an array of sheet names to hide more than a sheet at once). Setting visibility to xlSheetHidden or to False is identical.
b) Hiding a sheet and protecting a sheet are two different, independent things. You can protect a sheet but not hide it, you can hide a sheet but let it unprotected.
c) The main idea of protecting a sheet is to allow user input only at specific cells. The user can change the content of the sheet, but only to cells which are not formatted as "Locked".
d) If you protect a sheet via Excel (no matter if hidden or not) and want to modify something via code, you will need to unprotect it (and protect it again after the code is done). However, when protecting the sheet via code, you can specify that you want to allow the code to make modifications by setting the UserInterfaceOnly-parameter:
Thisworkbook.Sheets("Data Sheet").Protect Password = "IWontTellYou", UserInterfaceOnly:=True
e) If you never want to show the sheet, set the visibility not to hidden, but to veryHidden. With that, the sheet cannot be made visible by the use from within Excel: It will not be listed under "Unhide..." - in that case there is not need to protect it.
Thisworkbook.Sheets("Data Sheet").Visible = xlSheetVeryHidden
(Note that in that case you can make the sheet visible again only via code, but a one-liner in the immediate window is sufficient)
You will have to write a code to first unprotect sheet2, then paste the data and protect sheet2 again.

Is there a way using sheet protection to allow a user to enter values but protect formats?

I have a worksheet containing a column of numbers. The column is formatted with a background color, number formats, etc. The column is unlocked. I protect the sheet manually by right-clicking on the tab and selecting Protect. In the Protect Sheet dialog, 'format cells' is unchecked. I interpret to mean that the user should not be able to format cells. Yet when the user pastes into the column, formats are pasted along with values.
If I protect the sheet in VBA using
sh.Protect UserInterfaceOnly:=True
I get the same result: formats are pasted. I do not specify AllowFormattingCells:=False because the default is False.
I have seen posts suggesting that formats can be restored by copying and pasting them from a shadow area. I have used this solution before I started protecting worksheets and found it overly complex. I had hoped this was something protection would handle. If there is a way to handle it, I'd like to do it in VBA.
This question is a little old, but I had the same one, and so have many people in the past. With a bit of browsing I came up with a solution which seems quite clean and appears to work. Am I missing something?
Private Sub Worksheet_Change(ByVal rngTarget As Range)
Dim vPaste As Variant
With Application.CommandBars("Standard").Controls("&Undo")
If Not .Enabled Then Exit Sub
If .ListCount < 1 Then Exit Sub
If .List(1) <> "Paste" Then Exit Sub
End With
vPaste = rngTarget.Value2
On Error Resume Next
Application.EnableEvents = False
Application.Undo
rngTarget.Value2 = vPaste
Application.EnableEvents = True
On Error GoTo 0
End Sub
This could go in Workbook_SheetChange, but not if you already have code in Worksheet_Change, because the worksheet event handler gets called before the workbook event handler. But this code can go in a module to keep things tidy.
There is no built-in protection option you can use to achieve your desired result.
The only thing that works in this case is the clunky workaround that you mention, i.e. use a Worksheet_Change event that ensures the correct format after a cell has been modified.
Since there are many different ways to paste content, i.e. via various menus, ribbon commands, keyboard shortcuts, etc., any VBA solution that tries to intercept pasting will become very complex, much more complex than the change event that restores the format to its original state.
Another option might be user education and training (so they know to paste values only), although user behaviour may be the toughest element to change in the whole scenario.

Is it possible to always stay in activesheet when hiding another?

I have created a dynamic workbook where i put and pull data to more sheets containing data from other sources. I have a "Temp" sheet where code updates som data, this sheet is hidden until the macro is run, to put and pull data, i make it visible and when finished i hide it again. This works (almost) perfect.
The problem occurs when i hide it, then my last Sheet i selected automatically? My code is totally without any .select or .active (Still learning) because it is important that code always return to the sheet from where the code is run as this changes name everyday.
I can't seem to find anything that solves my issue, hope somebody can help:
I tried the following, as you can see in the following code:
Sheets("Temp").Cells.ClearContents
Sheets("Temp").Visible = False
'Cleans filters
ActiveSheet.ShowAllData
Application.Goto Range("A4")
Application.ScreenUpdating = True
I want to always stay in my activesheet (example: ("16") which is the actual date, there will be one sheet for everyday etc. ("17"), ("18") and so on.)
You can use With to immediately reactivate the sheet, like so:
With ActiveSheet
OtherSheet.Visible = xlSheetVisible
.Activate 'Immediately reactivate the ActiveSheet
End With
(You may want to toggle Application.ScreenUpdating so that you do not see so much as a flicker of the other sheet)
However, why do you need to show the sheet to copy data from it? If you are not using Select (which you say you are already sorted on), then you should be able to do most things with a Hidden sheet, such as HiddenSheet.Range("A1:B3").Copy
(A "Very Hidden" sheet, on the other hand, has a couple of restrictions - for example, you cannot delete a Very Hidden sheet.)

Excel Macro, Sending user's data

So I am currently studying SQL Server but right now I am just working a standard office admin job while I'm studying.
I never really made macro's before and little knowledge on VB but decided to design a macro for work to make things a bit easier for my team.
The macro just very simply allows the user to enter data, stats etc and gives the percentage or average statistic resulting in a total letting the user know if the statistics have been hit that day, week, month etc.
It works well but I would like to add a "SUBMIT" button that when a user clicked it would send the data they have entered in specified cells to myself. I am not sure how to go about it, If needed I don't have access to systems like SQL, Visual Studio etc in work as said just basic admin job at the moment.
Would It need to be submitted as a CSV? or could it be submitted from the user's sheet straight onto another macro I have designed giving the results for the whole team? As said I am totally new to this idea.
Cheers Guys.
Awright, according to what you may need in a very simple approach, the first thing you need to do it's to know the cells where they're going to enter info (care with ranges ), let's assume for this example that whe only had one data entered in the first cell of the team worksheet. So, create a button called 'button1' or as you wish and on the click event use this code :
Private Sub button1_click()
Teamsheet.Cells(row,column) = Yoursheet.Cells(destinyrow,destinycolumn)
End Sub
That would copy the value from one sheet to another, now, if you had you sheet locked via password, you must unlock it before doing that,then lock it again so code would be like this :
Private Sub button1_click()
On Error Resume Next
yoursheet.unprotect password:="yourpassword"
Teamsheet.Cells(row,column) = Yoursheet.Cells(destinyrow,destinycolumn)
On Error Resume Next
yoursheet.PROTECT password:="yourpassword"
End Sub
I clarify that this is a very simple approach, so, if you're using specific cells you can copy one by one and this would do (so you can make anny calculation son your admin sheet), but when you're copying ranges should be like this :
Teamsheet.Range("A1:D3").Value = yoursheet.Range("A1:D3").Value
Also, always consider how they enter this data you need.
UPDATE :
Let's say you have a team workbook and yours is admin_workbook, concept it's similar. This code will do what you need but both workbooks should be at the same folder or path :
Private Sub button1_click()
Var_data = Teamsheet.Cells(row,column)
Application.ScreenUpdating = False
Workbooks.Open Filename:=ThisWorkbook.Path & "\admin_workbook.xls"
ThisWorkbook.Activate
Admin_sheet.Cells(destinyrow,destinycolumn) = var_data
Workbooks("admin_workbook.xls").Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub
First you capture data on a var, then you open your admin book, put the data on the cell you want and close that workbook saving changes (you decide if you keep this line or mantain the workbook open and save manually). Also, Application.screenupdating it's a line that helps your screen doesn't flick when changing between workbooks.
Hope it helps friend !

How to prevent user from deleting sheet, but leave all else open

I have developed an Excel Application in which useres can enter custom categories. I let the user call up and hide this data entry worksheet with a button in the custom ribbon.
Now I've realized that the user can accidentally delete these worksheets. How do I disable the delete command for this worksheet while leaving all else open?
I've been searching the web but have come up empty on this.
This is for Excel 2007
Thanks
Protect them.
Tools > Protection > Protect Worksheet.
Add the password and choose what actions your users should do in the sheets.
You can do the same using VBA too. Check the following link
Updated with a code for sheet level protect
You may put the following code in the sheet that you need to manage any mischief ;)
Private Sub Worksheet_Activate()
ThisWorkbook.Protect Password:="Password", Structure:=True
End Sub
Private Sub Worksheet_Deactivate()
ThisWorkbook.Unprotect Password:="Password"
End Sub
But you see, when you have a book with 100 sheets and if you want 50 sheets to be protected.
Then you gotta either save all the sheet indices into a very hidden sheet. Usee that list in a module level VBA code to trigger the protect. Because not everytime you will have sheets in asceding order. If sheet indices in an order you can simply iterate them.
Let me know if you like to have workbook level code as well.

Resources