Set function of Excel Cell via Interop - LOCALIZED - excel

I want to add an Excel Function to a Cell via C# and Interop (Office 2010).
Let's take SUM for example.
Using WorksheetFunction ist NOT what I want, because this is a one time function call.
So what I want is
Xl.Range range = SomeWorksheet.Range["A1"];
range.Formula = "=SUM(A2:A5)";
This is fine if you are using an english version of excel but is an error in every other language.
I found this overview but this would mean to handcode the localization. Not so very nice.
What I need is s.th. like
string localizedFunctionName = Xl.Application.GetFuncName(Func.Sum);
Kind Regards
Martin

Try the following:
range.FormulaLocal = "=SUM(A2:A5)";
Read more here: http://msdn.microsoft.com/en-us/library/office/aa205983%28v=office.10%29.aspx and this question: VBA: cannot automatically recalculate Excel formula after updating it -- needs manual interaction

Related

How to make double line chart not rounded in the end (how to change cap style)?

When I create a line chart in Excel and change line style to double, I can see something similar to the image below:
As you can see the beggining and ending are rounded. I want to get rid of that, so it looks like this:
It can be done by changing line "cap type". I have tried exploring Series.Format.Line methods and properties but all I can do is modify beggining/ending arrows, there is no property/method for cap. I have also tried to use excel macro recorder to try and somehow expose this property/method but I have not been successful. I have also tried to search through various Excel/VBA forums but nothing helped.
Anyone able to help with my issue? I am looking for solution in VBA and VB.NET.
It seems that this property is not exposed in VBA, so probably you are out of luck.
According to https://social.msdn.microsoft.com/Forums/en-US/577b74db-9f9d-400c-a419-1999809922da/change-chart-data-series-line-format-cap-type-in-vba:
The cap type and join type are not exposed in the VBA object model in Excel 2007 and 2010 (I don't know about Excel 2013), so the only way to set them in VBA is using the notoriously flaky SendKeys to control the Format Data Series dialog.
The article talks about office 2010, but I coudn't find any clues that this changed later.
The object that should have this information is the Format.Line-property of a data series or data point (class is LineFormat), but it shows no cap-property.
The (not so) funny thing is that the enumeration for the possible values is available in VBA:
Try this sequence of formatting (example of formatting the first series):
Dim testSeries As Series
Set testSeries = ActiveChart.SeriesCollection(1)
With testSeries.Format.Line
.DashStyle = msoLineSquareDot
.DashStyle = msoLineSolid
.Style = msoLineThinThin
End With

How to determine the type of input variable in a password protected Excel add-in?

I am currently trying to fit some kinetic parameters for a Chemical plant simulation project. My main software is Aspen Plus. While it is a great environment, it's parameter fitting capabilities are underwhelming, getting easily stuck in local minima.
My next option is to use Excel's solver to do the parameter fitting. I've written some macros to do the formatting and found some VBA code to run the numerical integrator. Aspen has an Excel add-in to calculate some properties, which is great for the simulation. I've written the user defined function as the ODESolver requests; I'd like to include a function from the Aspen add-in within it. I've already added the reference. However when I try to call it from another function or as a simple Sub, it always returns either runtime error 9, 13 or 424, depending if I try to use a single cell or a range. Even worse, the add in is quite finicky and stops working when there is an error, so you have to close Excel and reopen the Workbook, which takes 1-2 minutes to load the Module.
I've included a simple demonstration of how it works within the spreadsheet.
Aspen Properties Add-In example
For instance, the VaporPressure function is described in the object editor as VaporPressure(ComponentList, temperature As String). Here's a simple example that fails.
Sub PropertiesTest()
Pres = VaporPressure(Range("B1").Value, Range("D2").Value)
Range("B4").Value = Pres
End Sub
And I get error 13. I don't really get this function. It might be a very obvious Dim problem but I'm too much of a newbie to really get it. Could someone help?

Which is faster in Excel VBA: InStr or Find?

I've got a function which uses InStr() to locate a character in a string, but I know there is a built in function in Excel called FIND(). Can anyone advise which is faster or more cpu efficient?
hours_position = InStr(1, value, " ")
vs
hours_position = Application.WorksheetFunction.Find(" ", value, 1)
Both are same and it does the same action.
Most (but not all) worksheet functions can also be called from VBA. For example, you can use the VLOOKUP worksheet function in VBA by calling Application.WorksheetFunction.VLookup (or Application.VLookup).
Similarly, you can use Application.WorksheetFunction.Find and Application.WorksheetFunction.Search. You can use them to emulate the way the worksheet functions work in your VBA code.
These functions are only available in Excel VBA, whereas InStr is a generic VBA function, available in all Office applications (and in VB6, VB.NET etc.)
Apart from that, the Range object in Excel VBA has a Find method, and the Worksheet object has a Find object. These, however, serve a different purpose: you can't use them to search for text within a string, but to search for cells with specified content.

VBA - Excel - Filling a cell with a nested IF

I'm using VBA to hack and splice together a macro to update a quickbooks exported report. I'm using a nested IF formula to create a filter for myself.
The formula I'm using is:
=IF(ISBLANK(I2),IF(ISBLANK(I3),IF(ISBLANK(H2),IF(ISBLANK(H3),"X",""),""),""),"")
(This will fill a cell with an X, if the 4 referenced cells are blank, and do nothing if any of them have a value).
The code I tried is this:
Range("N2").Formula = "=IF(ISBLANK(I2),IF(ISBLANK(I3),IF(ISBLANK(H2),IF(ISBLANK(H3),"X",""),""),""),"")"
The problem I'm having is that the " before the X is being read as the end of the code (I think); I'm getting a compile error.
Anyone able to help me out a little?
When you are using excel formula in VBA code, you have to careful with double quote:
Check the below code, it will work for you:
Range("N2").Formula = "=IF(ISBLANK(I2),IF(ISBLANK(I3),IF(ISBLANK(H2),IF(ISBLANK(H3),""X"",""""),""""),""""),"""")"

Value in a cell from CellReference in C#

I am using Office Excel VSTO. I have a cell reference info of a cell in my workbook. say, Sheet1!$A$5. I want to get the information in this cell and its type. Is this possible in VSTO by any means?
I am now breaking this cell reference going to the sheet and cell get getting the values. I suppose more easy ways are possible.
I am not sure if this is what you are after, but the following gives you direct access to the cell:
var range = (Range)Globals.ThisAddIn.Application.Range["Sheet1!$a$5"];
var cellContent = range.Value2;
Ok I think I have your question figured out.
You want to use VSTO to replace a workbook link that you are using to update a value in your current workbook.
For me the clues are ....
1. want to get the information in this cell and its type
2. use VSTO to do it
3. am now breaking this cell reference
btw if my assumption above is correct, then please edit your question to make more sense for future readers.
Code Sample
//get workbook link cell ref
var range = (Range)Globals.ThisAddIn.Application.Range["Sheet1!$a$5"];
//determine type
// if straight linking a value this step is unnecessary unless using the type info to format the cell
// or because you are doing a transformation or aggregation on the data prior to putting it somewhere.
// if needed... do some try/catchs on casting it to oledate, bool, double, string in that order.
// get value
var value = range.Value2;
// update "active" sheet
var sht = (Excel.WorkSheet)Globals.ThisAddIn.Application.ActiveSheet;
sht.Range["A1"].Value2 = value;
// don't forget to call FinalReleaseCOMObject and GC.WaitForPendingFinalizers/GC.Collect block!!
Also note that yes, you will be "breaking the cell reference" if you use code INSTEAD. Note you could keep the workbook link, but then there's no point in using the code approach. My advice would be in general use code since it is more flexible, but take advantage of links when you want speed (of configuration) and the data doesn't require more than basic manipulation (SUM, IF, basic math operators).

Resources