I know how to lock and protect selected cells in a sheet. But are there any way to do it in one sheet, and then it counts for all the sheets. I want to lock the selected yellow cells, in all the sheets.
This can be done in one go using the UI:
Select all the sheets (shift-click on the tabs)
Select the range on one sheet (which selects it on all the selected sheets)
lock the cells (this does the same for all selected cells)
Unfortunately the VBA Range object does not support "3D ranges" spanning multiple sheets so your options within code are:
loop over the sheets and set the Locked property of the appropriate range for each sheet
code the equivalent to the UI version above (using Select. This works as Selection can include ranges from multiple sheets)
I would always go for the first option in code - using Selection is slow and very prone to bugs see this question for more. And I can't think of any reason you would need to do it "in one go" when a loop is readable, fast and reliable.
Related
I have a Workbook with 3 different sheets (A, B, C). I create them with the POI in the same order and the whole creation process works quite fine. Somehow I want to have the last sheet as the active one. Therefore I set
workBook.setActiveSheet(2);
When I open the workbook with Libreoffice 7.1 than C is displayed as the active one. When I open print preview the first sheet (A) is always included. Changing the code to set as well C as the selected one won't help.
Is there a way to fix it for Libreoffice? (to have not A included in the print preview)
In Excel as well as LibreOffice Calc multiple sheets can be selected in a workbook. That's a wanted feature because then all settings are applied to all selected sheets. So, in GUI, you can set sheet properties - page settings for example - to multiple sheets the same time. Also changes to cells in GUI are applied to all selected sheets. So you can change cell formatting as well as cell values in multiple sheets the same time.
That's why the Workbook.setActiveSheet will set the active sheet - only one sheet can be the active one - but will not deselect sheets. To deselect sheets, Sheet.setSelected(false) needs to be used.
So if you want only the active sheet shall be selected, you need deselect all others.
Using HSSF Workbook.setActiveSheet also does not set the active sheet selected. So you should do this using Sheet.setSelected(true).
In your case:
...
workBook.getSheetAt(0).setSelected(false);
workBook.getSheetAt(1).setSelected(false);
workBook.getSheetAt(2).setSelected(true);
workBook.setActiveSheet(2);
...
I have a large file with a Scenario Manager, where changing a single cell on the Summary worksheet changes the visible scenario throughout the rest of the workbook. Data Tables are working a treat providing the headline values for each option.
I'd like to have a drop down on each sheet that when changed will change the same single cell on the Summary worksheet, so I don't need to go back to the Summary sheet every time I want to switch visible scenarios.
This is a simple process if I'm using macros and would be the solution I'd normally jump straight to. But this needs to be done without macros and this is where I'm now struggling.
Does anyone know if this is possible (without macros) and point me in the right direction?
Josh
You can insert combo box (Developer Tab > Insert > Form Controls > Combo Box) on each sheet. Mention linked cell as a cell of the summary sheet (Absolute reference with sheet name). That cell will give you index of the item selected in the drop down list. Then you can insert index formula in the cell you want to change every time to get value of the drop down list. Once you insert it on one sheet you can copy it to other sheets. No macros required.
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.
I have a VBA enabled excel workbook which has more than 20 sheets. Now the code in the VBA is using Worksheets numbers to call the worksheets.
Now I will have to delete one sheet which makes all the code unusable because if I delete one sheet the worksheet number changes.
Example Worksheets(10).Activate will point to another worksheet since I will be deleting the third worksheet
I am fairly new to this, so what would be the fastest way instead of manually changing the worksheet number in the code.
Note that there are 3 ways to access a sheet …
by its position in the tab bar:
Worksheets(10) gives the 10ᵗʰ worksheet in the tab bar.
Note that if you also have eg chart sheets the numbering is different from Sheets(10) which then might be a completely different sheet.
by its visible tab bar name:
Worksheets("Sheet10")
by its VBA name:
Sheet10
Note that the VBA name can only be changed in the Project window of the VBA-Editor.
Note that Worksheets(10), Worksheets("Sheet10") and Sheet10 can be completely different sheets, because they use 3 completey independ naming systems.
Which one to use highly depends on what you are actually trying to do. There is no best option in general each of them has their advantages and disadvantages.
Additionally I highly recommend not to use .Activate and .Select at all. Therefore read How to avoid using Select in Excel VBA.
Anyone know how to break out the tabs into separate workbooks BUT keep the locked cells locked like in the original file??
This is MULTIPLE tabs... I usually use Kutools to breakout 65 tabs or so into individual workbooks, but I need say the top cells to remain protected. Idk if this is even possible.
Right-click on the sheet name:
Select Move or Copy. Then select to new book in the second pop-up:
This will keep all formatting in place.