Is it possible to open a worksheet in excel from matlab and edit the formulas? The idea is to automate an uncertainty analysis by creating a second sheet with the uncertainty in each cell for the value from the previous cell. Essentially, I want to treat the cells as variables and do SQRT(SUM(Partials(xi)^2)) for each cell. Matlab should have no problem with the calc, but can it edit the formulas in sheets?
The process currently is to copy and paste from excel to matlab. Here's a small function that does the uncertainty in matlab against on array of equations:
function [f_u_total f_u] = uncertAnalysis(f, vars, vars_u)
f_u = [];
f_u_total = [];
for(i=1:length(f))
f(i)
item = uncertAnalysisi(f(i), vars, vars_u);
f_u = [f_u; item(1)];
f_u_total = [f_u_total; item(1)];
end
end
function [f_u_total f_u] = uncertAnalysisi(f, vars, vars_u)
f_u = [];
% take the partials and square them
for i=1:length(vars)
f_u = [f_u; vars(i) (diff(f, vars(i)).*vars_u(i)).^2];
end
% calculate the RSS
f_u_total = (sum(f_u(:,2))).^.5;
end
As an aside, the equations look something like this (why I'm not doing this by hand):
=(9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^4/C!Y3^6/(C!U3^C!Z3)^6*F3^2+1/4*C!S3^2/C!V3^4*C!W3/(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*O3^2+1/4*C!S3^2/C!V3^4*C!W3/(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*P3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^4*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*Q3^2+1/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*S3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*C!Z3^2/C!U3^2*U3^2+4*C!S3^2/C!V3^6*C!W3*(C!O
3-C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*V3^2+1/4*C!S3^2/C!V3^4/C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*W3^2+1/4*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3^3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*X3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^8/(C!U3^C!Z3)^6*Y3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*LOG(C!U3)^2*Z3^2)^(1/2)
You should be able to do it through COM/ActiveX/Automation. Look at the External Interfaces document; there's an example for how to access Excel documents through Excel's Automation interfaces.
I have next-to-no experience manipulating Excel in this manner, but I know you can do just about anything in Excel through Automation and editing cell formulas doesn't sound that hard.
edit: I can't find a reference to the Excel object model, but here's another example: http://support.microsoft.com/kb/301982
This isn't a terribly elegant solution, but if you save a new .xls spreadsheet that's simply a tab-delimited (or CSV) file, you can have Matlab generate formulas and when Excel opens the document the values will populate.
In Perl, I've handled it something like this:
open(OUTPUT,'>tmpfile.xls');
print OUTPUT "1\t2\t=A1+B1\n";
close(OUTPUT);
And when tmpfile.xls is opened in Excel, cell C1 will display as 3, which will dynamically update appropriately if A1 or B1 are changed.
(I'm not good with Matlab, so I have no knowledge of any sort of plugins)
EDIT: My previous assumption that XLSWRITE wouldn't work was wrong. I just tried the following in MATLAB:
xlswrite('xltest.xls',{'1' '2' '=SUM(A1,B1)'});
and when I opened the file in excel, the function was in fact there! The limitation on this would be that you would have to use only the functions that are in Excel.
Unfortunately, I don't believe XLSREAD can read the formulae into MATLAB (it appears to just get the result).
PREVIOUSLY SUGGESTED OPTIONS:
You may want to check out the Spreadsheet Link EX software on the MathWorks website, although I'm a bit unfamiliar with it and am not sure if even that can do what you need. Something else that you should look into is MATLAB Builder EX, which "lets you integrate MATLAB® applications into your organization's Excel® workbooks as macro functions or add-ins". Sounds promising...
Use COM/ActiveX. You can open an Excel instance via the following command:
xlApp = COM.Excel.Application;
Then use a combination of code completion and the VBA help in Excel itself to work out the rest.
Remember to close Excel with
xlApp.Quit;
delete(xlApp);
On a side note, so-called CSE (Control-Shift-Enter) formulae may help? See Google.
As an alternative, see the code below (xlswrite) for using ActiveX from Matlab:
http://www.mathworks.com/matlabcentral/fileexchange/2855
Related
I have an excel template that has preset formulas, then have my wpf application fills in data in other sheets then the preset formulas takes the data from the other sheet and shows it on the main page.
The problem is when I automatically PDF the excel most formulas go through but other's give me a System.Object[][] or some other errors. But when I access the excel file with the dataset it works.
The difference between the formulas that go through and the ones that don't are the ones that have an if() statement to remove all non zeroes in a range like this.
=TEXTJOIN("
", TRUE, TEXT(IF(Details!O:O>0,Details!O:O,""), "HH:MM"))
Functions like this works:
=TEXTJOIN("
",TRUE,Details!D:D)
How do I get Spire.xls to PDF the right format?
As you tested the Excel file and found it working fine, the problem is likely located inside the Spire rendering.
You can of course report a bug over there but that might not get resolved instantly.
You may want to try applying a number format to the cells for zero values instead of using that if part in the formula.
Something like this could do when writing with ClosedXML if implemented correctly by Spire:
worksheet.Cell(row, column).Style.NumberFormat.Format = "#,##0;[Red]-#,##0;\"\";#";
Formating possibilities are somewhat documented by Microsoft, take a look over there.
I am attempting to insert formulas into an excel spreadsheet using python.
Examples of the formulas are:
=VLOOKUP(B3|"Settlement Info"!$B$2:$R$2199|17|FALSE)
=SUMIFS("Payment and Fees"!$I$2:$I$6445|"Payment and Fees"!$B$2:$B$6445|Summary!$B3)
=+E3-F3-G3-I3
=IF(AND(I3>0|I3-N3>=-0.1|I3-N3<=0.1)|"Yes"|"No")
I tried using xlsxwriter and when opening the ss in excel it repairs by removing the "unreadable" content and those cells show as 0. I've seen the comment that the recalculation should be done on the reopening of the sheet when using xlsxwriter but that does not look like is is being done (https://xlsxwriter.readthedocs.io/working_with_formulas.html)
Is there some way to get these formulas into excel without them being removed by excel on opening?
Thanks for any pointers.
I simplified this down to a simple as possible:
When I run the below code and then attempt to open the excel spreadsheet I get an error saying "We found a problem with some content in ...Do you want us to try to recover..If you trust the source select yes"
If I select yes then I get an error " Removed Records: Formula from /xl/worksheets/sheet1.xml part"
And if I continue the sheet opens and there is a 0 in the field.
from xlsxwriter.workbook import Workbook
workbook = Workbook('test.xlsx')
worksheet = workbook.add_worksheet('Summary')
worksheet.write_formula('A2', '=VLOOKUP(B3,"Settlement Info"!$B$2:$R$2199,17,FALSE)')
workbook.close()
If I look at the information at https://xlsxwriter.readthedocs.io/working_with_formulas.html there is the information:
XlsxWriter doesn’t calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened.
This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don’t have a facility to calculate formulas will only display the 0 results. Examples of such applications are Excel Viewer, PDF Converters, and some mobile device applications.
Which I may not be understanding as I believe that the formula should be left in the sheet.
You can XlsxWriter to create any formula that Excel can handle. However, you need to be careful with the formatting of the formula to make sure that it matches the US version of Excel (which is the default format that formulas are stored in).
So your formulas should probably work as expected if you use a comma instead of a pipe:
=VLOOKUP(B3,"Settlement Info"!$B$2:$R$2199,17,FALSE)
=SUMIFS("Payment and Fees"!$I$2:$I$6445,"Payment and Fees"!$B$2:$B$6445,Summary!$B3)
=IF(AND(I3>0,I3-N3>=-0.1|I3-N3<=0.1),"Yes","No")
This one should work without modification:
=+E3-F3-G3-I3
See this section of the XlsxWriter docs on Working with Formulas.
Update in relation to the updated question:
The formula still has an error. You need to use single quotes instead of double quotes. You also need to add another worksheet for the formula to refer to. Like this:
from xlsxwriter.workbook import Workbook
workbook = Workbook('test.xlsx')
worksheet = workbook.add_worksheet('Summary')
worksheet.write_formula('A2', "=VLOOKUP(B3,'Settlement Info'!$B$2:$R$2199,17,FALSE)")
workbook.add_worksheet('Settlement Info')
workbook.close()
There is still an #N/A error in Excel but that is related to not having data in the VLOOKUP range:
Output:
I have a template presentation on powerpoint.
I would like to replace automatically generic placeholders with the value I input in an linked excel worksheet.
I started looking at VBA at the begining of the week, I am not really yet familiar with the syntax, but I am optimistic I will find a way to understand it.
I have put "[ ]" everywhere I want a value input:
the excel sheet present itself like this:
I would like to have some pointers (first, to know if it is indeed possible in powerpoint) to get me started in the right direction.
Does it need to be a PublicSub()?
Am I writing the macro on Powerpoint or in Excel?
Can I use a fonction similar to LookupV to get the values to replace the placeholders?
I would imagine something like :
(side question : can I use regex inside of VBA?)
for each "\[.\]" '(or characterString.startsWith("[")
lookup characterString in excel, return column 3
end each
Would this be possible?
Thank you in advance, I really appreciate the help.
Found an approximate solution, but simpler than code writing.
Open Excel,
fill the values, and copy them onto the powerpoint presentation : use the special paste option between the cell (or cells/cell range) you want.
Choose the "paste the link" option together with object worksheet Excel, select Ok.
Both files will be linked. You can change the values in Excel, and they will change also in powerpoint. The formating is done on excel.
In order to update all the values in the powerpoint after changing them on excel, powerpoint needs to be re-run, and at the restart of the application, accept the updating box prompt.
I find this simpler than the other solutions.
I want to print a row in Excel.
One row contains data for one project.
I want to print one row on one page nicely formatted. Meaning the value of one cell is the header and should be printed fat and centred, the other values should also be placed at fixed positions on the page.
Is this with VBA possible? When I was searching for this problem I only found results for printing a worksheet or a table or parts of it, but no results to use the values of the cells and formatting them.
Thanks in advance
As the other answers indicate, it is certainly possible in Excel VBA, but it is not really Excel's strong point.
What would typically be done to obtain the result you seem to be after is use a fully formatted Word document with fields that are then filled in with values from an Excel worksheet. You can even cheat a bit and use the Mail Merge \ Letter wizard to set everything up.
If you do want to do it all in Excel, you can find instructions and an example VBA macro here:
http://www.tek-tips.com/faqs.cfm?fid=4223
Template is a good way to do. With a macro there's better performance where it avoids the usage of volatile functions such as INDIRECT() However again it depends on how many volatile functions your worksheet carries.
Yes, it is possible when you use the Styles in excel. I know you can do Font formatting quite easily. Not sure about indenting it, but worth a try.
If style doesnt support it (it might in Excel 2010), you can always indent it via VBA (record a macro when you indent the values , it should look like this):
Selection.InsertIndent 1
I'm using NPOI to generate XLS spreadsheets. NPOI is an Excel Spreadsheet generation library/API that is available on codeplex, enables you to create workbooks, formatting, formulae and so on and so forth....I use it to create workbooks with multiple sheets that contain the output of the various calculations.
I've used the following custom data format for each cell that contains a value, which I've plumbed into NPOI using code along the lines of:
var newFormat = MyNPOIWorkBook.CreateDataFormat();
var customFormat = newFormat.GetFormat("[=0]0;0.####");
the customFormat is then applied to a Cell within a method that creates the Cell Styles.
This works rather well - but not so well when the 4th decimal place after the point/period is a 0. When this is the case, I do not get the 0. My requirement is to have the 0 actually show!
So, to illustrate. 0.33445566 displays in the spreadsheet as 0.3343 (fine!) - but 0.3340 displays as 0.334 (not fine) - I want 0.3340. I appreciate that this is somewhat trivial, but I would like to satisfy my objective precisely :)
Can anybody help - either by suggesting a workaround, or altering my custom format in some magical fashion?
Thanks
-SB
How about [=0]0;0.###0?