Excel VBA: passing arguments - excel

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)

Related

Adding formulas to excel spreadsheet using python

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:

Excel TEXT() Formula Getting the Month returning "mmm"

I got a report in excel and I'm having some trouble with a Russian PC. The formula TEXT is being used to get the month name from a date but, in the Russian PC it's not working and not given an error.
The formula is =TEXT(D7, "mmm") and the result is "mmm".
I also tried (The excel way to handle dates in different languages):
=TEXT(D7, "[$-409]mmm") and the result is "mmm".
I've tested different date functions and they all working. (Sum(), month(), changing data format, etc).
Version: Excel 365
As it works in my pc, does anyone have any idea what might be causing the error?
First - The mmm should be MMM.
Then, the fact that the PC is in Russian is a bit irrelevant. What matters is the installation language of Excel.
You may try the following
write 43319 on range A1;
then write this formula =TEXT(A1,"MMM"), using the English M and not the Cyrillic ones. Although they look quite the same, they are different.
it should return Aug as the month;
I've found a way to fix it creating a new formula in VBA and it worked great.
Press Alt+F11 (to open the VBA editor) Then Click the menu item Insert > Module In the new VBA module, enter the following:
Public Function FMT$(ByVal Value, ByVal strFormat)
FMT = VBA.Format$(Value, strFormat)
End Function
To use this, simply type =FMT(A1, "MMM") instead of =TEXT(A1, "MMM").
Credits to https://superuser.com/questions/730371/how-to-prevent-excel-to-use-the-os-regional-settings-for-date-patterns-in-formul

Excel dynamic ranges from Access code and Formula separators

I am currently rewriting some old Access code that defined named ranges in an excel workbook.
The old code is as follow:
oWorkbook.Names.Add "NameOfRange", "='Sheet1'!$A$1:$L2000"
With oWorkbook a Workbook opened from Access.
Since the range (number of rows) can change, I planned to use dynamic named ranges using Offset and CountA functions, and wrote the following code to replace the previous one:
oWorkbook.Names.Add "NameOfRange", "=OFFSET('Sheet1'!$A$1,0,0,COUNTA('Sheet1'!$A:$A),12)"
but then excel send an error 1004 There is a problem with this formula.
The same line of code executed from an Excel workbook works fine, and give the expected result.
On my computer, I use mixed English/French international settings, and my Office 2016 is in English, so I have a ListSeparator as ;.
When replacing the , by ;, the new formula below works:
oWorkbook.Names.Add "NameOfRange", "=OFFSET('Sheet1'!$A$1;0;0;COUNTA('Sheet1'!$A:$A);12)"
but I need my function to work on every configuration, not on only French/English bizaroid settings.
I tried to specify the named arguments (Name:= ... and RefersTo:=...) but its not correcting the problem. When called from Access, the code still need local separators (and maybe local function names? no idea).
So I can retrieve the computer excel List Separator (Using oWorkbook.Application.International(xlListSeparator) and replace all , in the formula with it, but maybe I am doing something wrongly and there is a better way to do?

MS Excel HYPERLINK() function with dynamic link_location does not work - why?

I am trying to include the spreadsheet's own filename in an e-mail body dynamically created with the following formula including a VBA Module (?) function:
=HYPERLINK("mailto:"&"address"&"?subject="&B7&"&body="&B7&FullName(); "E-Mail")
The function is
Function FullName() As String
FullName = ThisWorkbook.FullName()
End Function
in a "module" of the document (I don't have a clue about VBA: ALT+F11, Insert Module. Apparently this is also called user defined function (UDF)).
The function works on its own in a cell, the HYPERLINK formula works without adding to its link_location the function, but together (as above), it evaluates to
#NAME?
Links:
Office docs: HYPERLINK function
possibly related, but somewhat reverse problem: VBA to open Excel hyperlink does not work when hyperlink generated with a formula
Hyperlink custom link_location formula is not dynamic
Very similar problem: Creating a custom hyperlink function in excel
Follow this answer from the latter link in the question (do not use HYPERLINK formula, instead a function changes the manually created link in the cell).

International macros in Excel

I'm trying to create an international Excel macro sheet.
These macros should work for many countries
I'm writing formulas in English using Range.formula, but it does not work for all formulas (?)
For example, I create
Range("D4").Formula = "=SUM(D7:D14)"
In Spanish Excel it works so is filled with "=SUMA(D7:D14)". Is correct
Other formulas fails, for example:
"=IF(R2=8;D7;D6)" 'In Spanish Excel I get error 1004
"=ISNUMBER(O2)" works =ESNUMERO(O2)
"=PRODUCT(O2;O3)" Does not work, error 1004
I'm with Excel 2010 and Windows 7 , and I cannot understand this strange behavior. Parameters are valid for all formulas
Any ideas?
Range.Formula expects English formula notation. This means not only English function names but also using comma to separate parameters and not semicolon.
So
...Formula = "=IF(R2=8,D7,D6)"
and
...Formula = "=PRODUCT(O2,O3)"
should work.

Resources