unprotect sheet not working in excel 2013 (vba) - excel

I have a workook in which userform will pop-up only if somesheets as present (say Sheeta). On clicking submit in userform will do and some calculation and store it in Sheetb. I need to lock some cells from editing in sheetb.
code used in submit click
Sheets("sheetb").unprotect password="test"
'code for calculation
sheets("sheetb").protect password="test" , AllowFormattingcells:=true 'This statement locks the unlocked cells
The above code works fine when pop-up the userform through button say (loadfile).
I don't know what mistake I made.
Please help me to get rid of this...
Note : I want to prevent cells Sheets("sheetb").Range("N10:Q20") only from editing. Is it possible to prevent cells from editing without protecting a sheet.
Thanks in advance!!!

Following my comment, if your goal is to protect only range "N10:Q20" on "sheetb" and leave the rest unprotected, then you can do that very easily: simply select the whole range on the worksheet (CTRL+A), press the right-mouse button and go to "Format the cells.."; select Protection tab and untick "Locked" box. Next, select your range "N10:Q20" and do the same procedure but this time tick the "Lock" box. Now, run your code and you will notice that only your selected range will be protected, all the other cells on "sheetb" will stay unlocked and editable.

Related

I want to create an add button

I am newbie in excel and I really need a button that if pressed adds a certain value to the cells I selected, is that possible?
You can add buttons to Excel by enabling developer mode:
On the File tab, go to Options > Customize Ribbon.
Under Customize the Ribbon and under Main Tabs, select the Developer check box.
Then on the developer ribbon you can click insert, then select a button to draw onto the sheet.
You will then be given the option to assign a macro to the button (Also later accessible by right clicking the button).
As for the VBA code of the macro, you would need to be more specific about what functionality you require, is the 'certain value' to be added to the selected cells always the same value?
Someone answered me in two minutes on another site with this perfect code for what I wanted, thanks a lot sir. It works like magic.
Sub Add_to_Selection()
If Not IsNumeric([E4]) Then
Exit Sub
Else
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value + [E4].Value
Next cell
End If
End Sub

On selecting a checkbox in excel the "Tab(s)" should get enabled/disabled or Appear/disappear

Can someone help me with the VBA code
On clicking a checkbox in excel the "Tab(s)" should get enabled/disabled or Appear/disappear
For eg. There is a tab name"Summary" and in Summary sheet1 I have a checkbox. On selecting the checkbox in the sheet1, the sheet 2 should get enabled/disabled or it should appear or disappear
Your help would be much appreciated
Paste this procedure in the code sheet of the worksheet on which you have the CheckBox, for example your Summary sheet.
Private Sub CheckBox1_Click()
Worksheets("Sheet2").Visible = IIf(CheckBox1.Value, xlSheetHidden, xlSheetVisible)
End Sub
There are two references to CheckBox1. That's the name of the check box - in the sub's declaration and the code. If it has another name change both references. Change the name of the worksheet you wish to hide. If you have several check boxes on your worksheet place identical procedures in the code sheet, each referring to another check box.
Note that there are two different kinds of hiding a sheet, xlSheetHidden and xlSheetVeryHidden. Both remove the tab from the bar below the Excel window. However, if you use xlSheetHidden the hidden worksheet can be shown by selecting Unhide from the Format Cells menu. If you choose xlSheetVeryHidden the sheet will not be listed there and the only way to show it again is by access to the VB Editor or code.

Userform needed only once

I'm a new VBA user. I have a workbook with multiple worksheets. Each one has it own userform for data entry. The userform shows when I click on the sheet. After I'm done entering data, the worksheet is populated and the userform closes (unload). All this works well. However, after the initial data entry is complete, the goal is to use the data on the worksheets for other applications and the userform is no longer needed. What is the code or the terminology to say the userform should not reappear again when the worksheet is clicked on? Currently, I red X out of the userform. If I click the command button to close, it repopulates and I lose all my data.
Thanks!
As A.S.H commented; you could store the information in a number of ways. An easy example is declaring a variable outside of the Macro:
Public FormOpened as boolean
And then set FormOpened as true once the form has been shown. Then you could add a check to the start of the mouse-click macro:
If FormOpened = True then Exit sub

Remove error indicating unlocked cells are protected with VBA

I have a template sheet that is copied through VBA into a new sheet.
The new sheet is added by clicking on a button that makes a userform pop up, and the information in the userform is transferred to the new sheet.
This new sheet contains locked headline cells as well as unlocked empty cells for the user to put information into. The sheet is protected but allows users to select locked and unlocked cells.
Everything worked for some months until the other day. When a new sheet is created and the user tries to fill in the unlocked cells, Excel responds as if they were locked!
The following information pops up:
"The cell or chart you're trying to change is on a protected sheet. To make changes, click Unprotect Sheet in the Review tab (you might need a password)."
This error disappears after the user does any of the following
1) double click in any empty cell (instead of just selecting it) and put in information this way and then click enter, then all cells behave normally.
2) activate any other worksheet and then return to this sheet
3) choose Format>Unprotect Sheet (this does not unprotect the sheet but it removes the error. If the user wants to unprotect the sheet then they have to click Format>Unprotect Sheet twice, i.e. first to remove this weird error and then to unprotect it).
I am trying, with VBA, to remove this error by doing any of 1, 2 or 3 above.
Any ideas how to solve this?
An additional problem is it is not possible to scroll with the mouse in the sheet while having the problem.
I tried the following code:
ActiveSheet.EnableOutlining = True
ActiveSheet.Protect Contents:=True, userInterfaceOnly:=True
After some googling I managed to solve this by doing the following:
Create a Module in VBA and add the following code:
Public Sub RefreshActiveCellSelection()
ActiveCell.Select
End Sub
Whenever you perform a Workbook.Activate or Worksheet.Activate, immediately call
Application.OnTime Now, "RefreshActiveCellSelection"
Another solution was to uninstall KB3085502
I found the answer here: http://answers.microsoft.com/en-us/office/forum/office_2013_release-office_install/microsoft-update-excel-2013-kb3085502-32-bit/0cc7d498-92cb-4478-9554-77cff286c847

Excel 2010 - Multiple Types of Locks on the Same Sheet

So I'm trying to create an excel sheet that handles a user input/output task (I know that this is a kludge at best and offensive at worst). So I want the user to be able to edit the input cells (and only the input cells), and select (but not edit) the outputs.
Example: Grid A1:C3. A1:A3 are completely locked - unselectable. B1:B3 are selectable and editable. C1:C3 are selectable and uneditable.
Is this possible? My gut is telling me no, but I thought I'd ask anyway.
It is possible using worksheet protection:
In the Format Cells dialog of the cells B1:B3, uncheck the "Protected" checkbox in the last tab (Protection). Then protect the worksheet (right click the worksheet tab at the bottom->Protect Sheet). Once the sheet is protected, the user can only edit those cells that are unprotected.
Regarding the selection of C1:C3 and non-selection of A1:A3 - you can allow/prevent the user from selecting protect cells in the same sheet protection dialog. However, this is a sheet wide setting, so be default you can only fully prevent selecting all protect cells or allow selection of all cells.
If you only want prevent selection of A1:A3 because you don't want the user to see the formula, just check "Hidden" in the Format cells dialog - this way the user does not see the formula.
If you really need to separate between selectable and unselectable, either split across two worksheets - or use a little VBA macro. To do so, open the VBA editor (Alt+F11) and double click your worksheet in the top-left list. In the code window, enter the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Me.ProtectContents And Intersect(Target, Range("B1:C3")) Is Nothing Then Range("B1").Select
End Sub
This way, every time a cell outside your required range is selected, B1 is selected instead

Resources