openpyxl: cells do not evaluate - openpyxl

I have this simple python script:
from openpyxl import Workbook
book = Workbook()
sheet = book.active
sheet["A1"]=3
sheet["A2"]=4
sheet["A3"]="=SUM(A1:A2)"
book.save("k_test.xlsx")
When I open the excel sheet A3 is empty (not calculated). If have to click on the cell and press enter - then the value appears. How can I force excel to calculate the values?

In case anyone runs into the same problem. There were two issues:
as I was starting from a non English Excel version, I forgot to translate one command to English. So it showed up right lateron in Excel, but there had been an internal error in between that kept it from evaluating it.
I had references to an Excel tab that did not exist at the time of inserting the formula. This also leads to the problem that it cannot be evaluated. I had to add the referenced tab, before I could add the formula referring to it.

Related

VB.net copy and paste excel cell to elsewhere in the workbook

So i am currently using Visual Studio to create a application that takes info out of an excel sheet and then does some calculations on the data and then pushes back to excel.
This bit i have managed to do but the bit i am struggling on is using a 'Parameters' sheet. I want to be able to enter a formula into a cell in one sheet of the workbook and then paste that formula into another sheet but to have it updating,e.g. as the cells go down the formula changes like it would in excel. I used a manual work around by hard coding the formula and then having variable as the row number, however i want to be able to just change the formula in the excel sheet and then when the code runs it applies to the rest.
Currently i have tried saving the cell value/text into a variable and then making the new cells equal that variable, however this then applies the same identical formula to the whole of the column(All required rows).
What i am currently trying to do is paste the variable into the top row and then copy and paste that cell down to the last one,
I have tried making the variable a formula but it evaluates the formula before it is equal to the variable and therefore just sets all the new cells to the formula answer, so i changed the cell to be text instead which then meant the formula did appear in the new cell however it was the identical formula for all cells.
The copy code works as below
bjExcel.cells(rown, colval) = param1
objExcel.cells(rown, colval).copy
This is working fine
But when i use the below the paste won't work
Do Until rown = 10
objExcel.cells(rown, colval).copy
rown = rown + 1
objExcel.cells(rown, colval).paste
Paste is not a recognized with the error:
System.MissingMemberException: 'Public member 'Paste' on type
'ApplicationClass' not found.'
Could be you need to use PasteSpecial instead?
https://learn.microsoft.com/en-us/office/vba/api/excel.range.pastespecial
Depends on what you're using to interop with excel.
shWorkSheet.Range("C7:C7").Copy()
shWorkSheet.Range("V7:V7").PasteSpecial(Excel.XlPasteType.xlPasteAll)

Reference external file with ^ in name

I am trying to pull data from external workbooks into a main workbook. I have done this before several times and it always works with the below formula.
='FolderPath[FileName]WorksheetName'!CellReference
The above formula still works for me now on many workbooks. However I have thousands of workbooks with a ^ in the name. Example Workbook1^01012017.
When I get to these types of workbooks I get the below error:
"The syntax of this name isn't correct." and the text in the cell gets highlighted until the first ^.
If I type in = in my original workbook
Open the workbook I want to reference
Select the cell I need
Hit Enter
The link works. If I copy that text exactly and change the reference from cell A1 to A2 in another cell, I get the same error. The problem seems to be that it's reading the ^ as a symbol instead of a text. Is there any other way to get this to work? I have tried using a indirect function, but those don't work when the external workbook is closed.
Any suggestions?

How to get defined table name in Excel?

I have an excel file with several sheets with assigned to them alliances. One of value on one sheet is calculating by using code below:
VLOOKUP(D10; lst_table_col; 4;00);
It doesn't matter that VLOOKUP function do, that matters is what exactly sheet behind lst_table_col. The problem is that this excel file doesn't contain any lst_table_col sheet. I'm looking for any solution: by just mouse, vba - whatever.
I believe my question is very easy, but this situation confuse me.
You must have a table called lst_table_col
Because if it was a direct reference to an Excel Range, you would see something like this in the formula :
SheetName!R1C1:R10C4
or
A1:D10
so check in Formula Tabs -> Name Manager to find it! ;)
Here is the link for the tutorial provided by #Rocketq : https://support.office.com/en-ca/article/Define-and-use-names-in-formulas-4d0f13ac-53b7-422e-afd2-abd7ff379c64#bmmanage_names_by_using_the_name_manage

Apache POI and SUMPRODUCT formula evaluation

I have a template XLS file that I load with Apache POI and write loads of data in it, then save it as another file.
I have formulas in my XLS file like this:
=SUMPRODUCT((DS!B:B="IN_THIS_ONLY")*(DS!D:D="New trade"))
also tried
=SUMPRODUCT(0+(DS!B:B="IN_THIS_ONLY"),0+(DS!D:D="New trade"))
these evaluate correctly if I press Enter on the cell in Excel.
However, simply calling
HSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);
does not seem to evaluate them, neither does pressing on the "Calculate now" button in Excel - so I guess this is a special formula or function.
The other, more conventional COUNTIFs and SUMIFs work fine, however these do not allow multiple conditions to be specified.
POI does not support array formulas.
Is there any way to make these work. I'm using POI version 3.7.
One can press CTRL-ALT-F9 to manually re-evaluate all formulas forcefully in Excel.
And here is the trick to make it work automatically on workbook open.
Add the following to your formula:
+(NOW()*0)
so for example, my SUMPRODUCT above becomes
=SUMPRODUCT((DS!B:B="IN_THIS_ONLY")*(DS!D:D="New trade"))+(NOW()*0)
And this works! Excel now recalculates my special formula cells on open.
The reason for this is that NOW() is a volatile function. Here is where I learned about this: http://msdn.microsoft.com/en-us/library/bb687891.aspx
Application.CalculateFull also works, but only in Excel 2007 and later (and of course, one must enable macros to run). Unfortunately, in my case even though I use Excel 2007 my workbook will be opened by Excel 2003 users as well, so this was not an option.
Is SumProduct an array based formula function?
If so, that would explain the issue. One option is to contribute a patch to POI to add the missing support. There's been some discussion on the dev list and bugzilla on what's needed, and if you were to post to the dev list then we'd be happy to help you get started.
Otherwise, you could just set the formula recalculation flag and get Excel to recalculate the value on load

Need to Update External References in an Excel Sheet while a Macro is Running

I have an excel macro that sets Cells to an external location.
Range(NamedReference) = "='http://webaddress/ExcelSheet.xlsx'!NamedReference
Other cells use that location to calculate new values.
"A1" = NamedReference + 1
The problem is that I need to read the new calculated values back into the macro to export data, but the external link has not yet been calculated to any value. It is a #NAME? until the macro is done running. Is there any way to force excel to get those values during the macro run time?
I have tried a variety of things including
Calculate
CalculateFull
Any help would be appreciated. My current solution is to just close the macro on error and have the user re run the macro, but it is really kludgey.
**Edit: Forgot equals sign in formula
You could try
ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources
See on MSDN