I need to make a template where the rows and columns cannot be deleted but the cells can still be edited
Select all the cells in the sheet and unlock them
Take a cell that doesn't need to be edited (there must be at least one somewhere on the sheet) and lock it.
Activate sheet protection
You're done
Related
I'm preventing the user from formatting cells in a worksheet in a generated Excel file by executing this code:
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("sheet1");
CTSheetProtection prot = sheet.getCTWorksheet().getSheetProtection();
prot.setFormatCells(true);
Is there a way to lock formatting for individual cells in a sheet as opposed to having to lock formatting for the entire sheet?
Likewise, is there a way to apply the other protections to individual cells?
For example, filtering and sorting - if someone tried to apply these operations on a group of cells that included a cell that was locked for that operation it would be disallowed.
In the Format Cells dialog box you can toggle cells locked or hidden. When you use "Protect Sheet" From the Review tab you can choose to protect the worksheet and the contents of locked cells, and choose what you want to allow the user to do to locked/unlocked cells. Password protect the sheet and users will not be able to remove the lock without your permission.
I'm assuming most of your cells are to be unlocked, so select all cells and set to unlocked and then lock the ones you want locked.
I am unsure of the code to do this through VBA, but it should be easy enough to find.
Logically, you would want to select all cells and then set the cells.format.protection.locked to false, and then select the ranges you want locked and set those to true. You could easily record a macro to find this code.
you should check this link, I have written a custom method to lock individual or range of cells in single or multi sheet excel file.
https://stackoverflow.com/a/63299413/6835092
for single cell locking just mention same range value. i.e a1:a1. this will lock only a1 cell.
1 1 2
ACT ACT FCST
I have three columns, I would like to run a loop code to check values in 2nd row if it is ACT or FCST. If it is ACT, it will protect the whole column so it will not be updated.
You don't protect columns, you protect sheet. Cells are set as locked or not locked. Then if the sheet protection is activated, locked cells cannot be edited. So to unlock cells to make them editable, you have to unprotect the sheet, set cells as unlocked then protect the sheet.
Use the macro recorder to help you create code. When you develop code that has issue, post it for analysis.
How can I stop a user selecting an entire row or column in excel? The problem is that user selects the row to take the data they want, but they also get a bunch of hidden cells etc. that I don't want them to take.
Lock the hidden cells, unlock everything else and protect the worksheet with the sole restriction of Select locked cells (allow everything else).
I have been using the following to delete a row.
Rows([2]).EntireRow.Delete
But this was when my workbook had only one sheet. A command button has been added in sheet2 which calls the macro present in sheet1.
How should I modify my above code so that the VBA deletes the second row of sheet1 ?
You only need to specify which which sheet the row is in, in addition to specifying the row as you already do. I.e.
Worksheets("Sheet1").Rows(2).EntireRow.Delete
Should do what you wish. If you have called the worksheet something different than "Sheet1", change what is inside the quotation marks. Strictly speaking you don't need to specify that it is the entire row you want to delete, as you've already said it is the row you are dealing with with Rows(2), but it shouldn't do any harm in there either.
If you additionally want to do the deletion from a different workbook (or just make sure that the deletion takes place in the correct workbook), you can also specify the workbook-name:
Workbooks("Workbookname.xlsm").Worksheets("Sheet1").Rows(2).EntireRow.Delete
If your button is in any other sheet and you wants to delete a row from another sheet, you simply have to activate that sheet. For example, you are trying to delete row from Sheet1 then you can activate the sheet by this one-liner.
Sheets(0).Activate
I am creating a template in excel where I only want the user to be able to enter information in some of the cells. For that reason I have locked some cells and unlocked some others and I have also protect the corresponding sheet.
The user wants to be able to copy information from other excel sheet to one of the unlocked cells of the template and be able to change the format of the pasted text, being bold and underline options avaliable.
Is there any way to do that without having to unlock the sheet?
Thank you in advance for your time.
There are options when you apply protection to the sheet to still allow formatting.
There is no issue copying and pasting into a locked cell as far as I am aware.
There is an option when you apply protection to a worksheet in Excel to allow "Format Cells". Choose this if you want them to be able to apply formatting to the cell. Likewise, Format Rows and Format Columns if you want the user to be able top adjust the row height and column width.