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);
...
Related
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 need help automating a daily task at work since I'm not a programmer. In short, I work with excel files and I am required to compare data between two workbooks for any duplicate values in any given columns (could be same or different columns) from both of the workbooks and hide the rows with the duplicate values in any one of those given workbooks. Since these workbooks are all different every time, is there a possible code that will allow me to hide the row with the duplicate values as needed on any workbook instead of me having to go through each row in every single workbook manually?
Excel has a tool to consolidate different list, sheets or workbooks. From a 3rd workbook go to the "DATA" tab, and click on the "consolidate" button as shown on the Fig. 1. Then, click on the "Browse" button, select the first workbook and click "add" button. Then repeat the same with the second workbook.Finally clik on the 3 check boxes and press ok. Please find below a link to an explanation of the consolidate funtion. If the funtion does not reads from the workbooks, you can try copying the two sheets you want to compare in one worbook
Example: https://www.youtube.com/watch?v=Lk7HPyOHr0E
I am creating a VBA code to select 3 specific tabs to convert into PDF. I want to be able to select 3 specific sheets from the list of sheets in "Microsoft Excel Objects" so that even if the order of the sheets is changed or sheets are renamed, the same tabs will be selected and converted into a PDF.
At the moment, I have this code to select the first 3 tabs. (which works fine)
Sheets(Array(1, 2, 3)).Select
I have tried the below (doesn't work)
Sheets(Array(Sheet1, Sheet2, Sheet3)).Select
Expected result is to be able to select 3 specific sheets from the list of sheets in "Microsoft Excel Objects" so that even if the order of the sheets is changed or renamed, the same tabs will be selected and converted into a PDF.
You need to use the names to do a multi-select. But you can refer to the codename to get the name: Sheet1.Name
Sheets(Array(Sheet1.Name, Sheet2.Name, Sheet3.Name)).Select
You would change the Sheet1 and others to the CodeName of the sheets you want to select.
One way is to assign a unique CustomProperty to each sheet you want to export:
Sub qwerty()
ActiveSheet.CustomProperties.Add Name:="PDF", Value:="pdf1"
End Sub
and similar for the other two sheets.
Then when you want to export, loop through all sheets looking for the proper CustomProperty. It won't matter if the sheet names have been changed or the CodeNames have been changed or the order of sheets has been changed.
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.
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.