Openpyxl: switching between formulas and computed values at the worksheet level? - openpyxl

I'm currently aggregating workbooks where I can get away with just copying the calculated results for about 80% of the worksheets. However, I need to preserve the formulas for the remaining sheets.
I'm currently having to read each workbook in twice, creating separate workbook objects for ones with formulas and ones with calculated values:
calcualted_wb1 = openpyxl.load_workbook(filename, data_only=True)
formula_wb1 = openpyxl.load_workbook(filename)
This seems very inefficient, and I am wondering, Is there a way a way to switch between calculated and formula values at the worksheet level?

No, it's a global setting when reading the workbook. Easy enough to have two workbooks one of which is read-only and data-only and the other is editable.

Related

Transfer all cells, formatting, formulas into new workbook for all sheets

My Excel workbook is getting incredibly slow to save, even though I have manual calculation on, nothing crazy going on in the vlookups, etc. But it spends so much time recalculating and I am wasting hours on this.
Is there any clean, easy way to just copy/paste all the contents of all the sheets (cell values, equations, etc) into a new workbook just in case the old one happens to be corrupted?
Check each sheet by hitting control+End. Does any take you to a row and or column far outside of your actually used range? If so, delete entire rows and columns past your REALLY used last cell (repeat for each worksheet) and then save a copy of your file.
Also, the xlsb file format is smaller so quicker to save and open.
Finally, if your wb is slow to calculate there is room for improvement. Any SUMIF(s) or array formulas in there? You say nothing crazy on the vlookup. What do you mean by nothing crazy? How many are there and to how many cells do they refer?

Excel: How to compare sheets from 2 different workbooks for differences

I have an original excel file that I have ran a simulation that inputs financial data. I made a copy of this file, and wired the formulas up differently to try and increase calculation performances.
I now have 2 workbooks, the original and the final. I want to compare each sheet from each of the workbooks together to make sure that the financial numbers have remained the same, to make sure the new formulas are not effecting the numbers received.
I have tried to put copies of the two sheets into one workbook, name them April12 and April15. Then insert a third sheet. In cell A1 of the third sheet, I wanted to use the formula
=April12!A1=April15!A1
to get TRUE/FALSE values. But the formulas in these sheets reference many other sheets that are not in this new workbook, so all of my numbers turn up as #REF.
Iv googled many different ways of approaching this but I cant seem to get any of them to work. Does anyone know a simple way I can compare just the values from 2 sheets from 2 different workbooks to find out if the numbers have remained the same or have changed?
Note:I am using excel 2010.
I think you already know how to verify data using formula so is the problem to refer to a row in a different workbook ? if so, following might be helpful :
=[yourFile.xls]SheetName!$Col$Row
this way you can update your formula like(yourFile.xls refers to the complete path including the file name) :
=[file1.xls]April12!A1=[file2.xls]April15!A1

in excel, running from vb.net, can I add the values of all the cells in one sheet to the cells in a different sheet?

I have a vb.net application that uses excel. One of the things it does is total the cells in a certain by a certain type, which for the purposes of this question is not relevant. The result is that the cells in sheeta are added to sheetb. This is a repetitive process as multiple copies of sheeta are processed and i want the total of 'all' of the sheeta cells to be added into sheetb. If there were 50 copies of sheeta, then the value of cell A3 in sheetb, for example, would be the total of the values in cell A3 of the sheetA. I am doing it as a cludge now by adding the value of each cell individually. I'd like to be able to use all of the cells in a few statements only, without doing addition statements repetitively.
Thanks for the help
You have a couple of options:
You can write code to cycle through the sheets to build a SUM formula in sheetb for each of the instances of sheeta
You can consider using a PivotTable that draws from multiple sources. Then you simply need to refresh the PivotTable whenever data changes in any of the source sheets. You can refer to this Microsoft Support Page for instruction on how to build it: https://support.office.com/en-ca/article/Consolidate-multiple-worksheets-into-one-PivotTable-report-3ae257d2-ca94-49ff-a481-e9fc8adeeeb5
In either case, you will need to be weary that if you add additional instances of sheeta (or remove them), then you will need to rebuild the formula in option 1 or the PivotTable in option 2.

Extending the number of reference cells passed to .Range function in Excel VBA

I am using worksheet.range("Relevantcells") to copy cells in a sheet. I have non-contiguous cells and hence it only lets me put in 60 specific cells.
How can I extend this to many more cells that I want to read from?
Instead of having one large non-contiguous range, you could create an array of smaller ranges in code and then copy each range iteratively.
You'll likely take a performance hit over copying the ranges en masse, but this approach should be more flexible.

Copy Excel worksheets, macros, and graphs from one workbook to another, moving links to the new workbook

I have an Excel workbook with a number of features:
One main user-facing sheet
One summary sheet based on the user-facing sheet's data
A number of graphs based on the user-facing sheet's data (as in, the type of graphs with a separate tab for them, rather than objects within a worksheet - I'm not sure if they have a special name or special properties)
A series of 'background' worksheets that calculate the values for the user-facing sheet
A macro to allow the user to sort the user-sheet by any column they wish, which is referenced in the user-facing sheet's Worksheet_SelectionChange event
However, for distribution I'd like to cull the sheets for simplicity (and file size - the entire data query is included on one of the sheets). I still need to calculate the values for the user-facing sheet, but it's only done once per dataset, so that can quite happily be copied as formatting then values.
The trouble, however, is transferring the dependent sheet, graphs and macros across to a new workbook so that instead of referencing the old workbook, they reference the new versions of the sheet. Ideally I'd like to do this with VBA or something, but my Google searches thus far don't seem to have turned up much of relevance.
Does anyone know how to do this?
I'm not sure I entirely understand your question, but what I think that you want to do is to create a new version of your workbook, with less worksheets in it??
To do that in VBA, this code snippet is a good place to start:
Sub Macro1()
ActiveWorkbook.SaveAs Filename:= _
[your path here]\Book2.xls", FileFormat:=xlNormal, _
Sheets("Sheet2").Select
ActiveWindow.SelectedSheets.Delete
End Sub
Creating a copy of the entire workbook, and then deleting what you don't need will make sure that the links do not reference the old workbook.
Once you have created the new workbook, with links intact, it is then pretty easy to remove all the things (calculations, etc.) that you don't need.