EXCEL to VBA code translation - excel

To extract the first coefficient of polynom which approximate a function, I use this formula:
=INDEX(DROITEREG(B2:B10;A2:A10^{1.2.3});1)
But when i want to use it as a VBA code like that :
a = INDEX(DROITEREG(B2:B10;A2:A10^{1.2.3});1)
I get a syntax error
Can someone help me to resolve this problem? Thank you

To use Excel formulas in VBA you must use Application.WorksheetFunction.<function name> as follows (if using inside of Excel, the Application. can be left out):
a = Application.WorksheetFunction.Index(DROITEREG(B2:B10;A2:A10^{1.2.3});1)
I was unsure how to implement the DROITEREG function and left it as is.
See MSDN for more info

Related

For Each Cell in Excel Macros

Hello I am facing some issue while running a macro in Excel VBA.
For Each cell In [Data!A2.A65536]
I was using the above code, and was getting type Mismatch error. When I replaced it to
For Each cell In Worksheets("Data").Range("A2:A65536").Cells
the error was gone.
Since I am not well versed in vba, can anyone explain what's the difference in the above snippets? What was the issue due to which the aforementioned issue was happening? and can someone please confirm, if I have correctly replaced the code.
The second one is "best practice" and is the one you should use.
The square brackets is just a shortcut notation for the range property (see Refer to Cells by Using Shortcut Notation) it is actually the same as using the Evaluate Method.
You get an error because you used a dot . instead of : in [Data!A2.A65536] it should be [Data!A2:A65536].
So using [Data!A2:A65536] is the same as Evaluate("Data!A2:A65536"). If possible I would avoid Evaluate or the bracket notation.

convert yearly data to monthly in R or vba

someone suggest the L'Aggrange interpolation, using Vba but i am not an expert in Vba if someone have been use this interpolation before please give any advise.
i try to make a function in excel vba but it's not correct
Doing a brief web search for VBA LAgrange interpolation brought up this file: LAgrange
You could start with that and modify it to fit your needs.

Executing build-in functions from VBA (Excel)

I need to have a capability to execute any kind of build-in functions (such as 'sum' or 'len') from VBA (MS Excel).
One of the restrictions that I have is that I may not pass cell ranges as arguments to these functions. Instead of this, I should be able to use strict values.
I want to be able to use the following expression SUM(1, 2) which should return 3, while the following version SUM("A1:A2") won't work for me.
I managed to develop some function that parses my prior input and makes it consist of a list of values (as for example above, it made the user's input of 'A1:A2' look like an array of numbers consisting of two values).
So, can anyone give me an example of using a build-in function that receives a list of values (not just range of cells)?
I tried the following code, but for some unknown reason, I haven't been able to get it working (I keep getting 1004 error, saying: Cannot run the macro 'SUM'. The macro may not be available in this workbook or all macros may be disabled.):
Application.Run "SUM", 2, 2
Some valuable advices that would help to find a solution to this problem will be greatly appreciated.
To use a built-in, Excel, Worksheet function, you need to do something like the following:
Application.WorksheetFunction.Sum(2,2)

Excel VBA: passing arguments

I am working on some Excel functionality using VB - but I am getting stuck at some examples.
Current version is Excel 2007, using a blank Workbook; I've added in a module and trying a function like the following:
Function Addtwo(a, b)
Addtwo = a + b
End Function
However, I get the error #VALUE! in my cell, when doing Addtwo(5,5). When trying to do Addtwo(B2,B3), Excel tells me my formula is wrong.
Thanks,
The pasted code is okay and works in my Excel 2007.
The only possible problems I can think of:
You forgot to use the equal sign: Addtwo(5,5) instead of =Addtwo(5,5)
Your language settings require a semicolon instead of a comma in the formula, i.e. =Addtwo(5;5) (in the worksheet formula only, not in the VBA code)

Modify an Excel sheet from Matlab

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

Resources