Change to Canadian (CDN) French number formatting in Excel VBA - excel

I would like to build a piece of code which can convert numbers in my workbook from English to French number formatting.
For example,
English formatting would be $1,000,000.99, and
French would be 1 000 000,99 $
I can not seem to find any default French codes built into excel and using a , instead of a . for the decimal does not show numbers less than $1.
Any assistance or advice is appreciated!

To make the currency symbol appear to the right of your numbers, use the NumberFormat property of a Range object and set it to:
For dollars: #,##0.00 [$$-C0C]
For euro: #,##0.00 [$€-C0C]
The behavior of commas and periods for decimals and digit grouping is usually controlled by your Region and Language Settings in Windows.
You can change this setting application-wide (i.e. it will affect any workbook opened on your machine) by changing the values of Application.DecimalSeparator and Application.ThousandsSeparator in VBA. Alternatively, you can achieve the same effect by going to File > Options > Advanced and uncheck "Use system separators"

To get the code for any action in Excel:
start recording a macro
do what you want to replicate via code
stop the recording
check the macro code (ALT+F11)
In this case applying the formatting gives this code:
Selection.NumberFormat = "#,##0.00 $"
Hope this helps.

Related

An issue when generating text with national symbols using VBA in Excel 2019

I am using some VBA add-in to generate text from numbers, that text contains national Lithuanian, German or any other national symbols. This used to work fine with the previous versions of Excel so far, but the system has now been upgraded from scratch and it does not seem to work well with Excel 2019.
In VBA, the code looks fine and you can see the national symbols like "š" (well after setting the locale to Lithuanian, before they were appearing as multiple strange symbols):
, however when in excel the functional is called, the output has just � instead of any national symbol. All of the national symbols are marked with the same � sign in Excel 2019. But you can write manually national symbols just fine in Excel, just what comes out of VBA is garbage missing those symbols.
It looks like this in a cell:
Trys �imtai �, 00 ct
Does anybody know how to fix this and make them appear normal with how they are visible within the code?
A few other observations are that when copying and pasting the code from the VBA Editor to Notepad++ the national symbols get lost as well and they appear as different strange symbols. Also when trying to use the find function in the VBA Editor window to find anything with the national symbols - no results are ever found as long as you don't remove the national symbols...
In my understanding, support for "international" ("non-Latin") characters is determined by your Windows Regional Settings, not by Excel or by the VBA IDE.
Since I'm in Canada I can't easily test on my end to verify this, but interestingly, your example character of š displays the opposite of how it shows for you: it renders properly on the worksheet but as gibberish in VBA.
Incidentally I determined the Unicode id number for š (Unicode 353) by pasting the symbol into cell A1 and then in another cell using formula =UNICODE(A1). The opposite function (to return a character from it's code) is UNICHAR. The VBA equivalents are AscW and ChrW.
I was aware that the VBA IDE can be made to display "international" characters by adjusting Windows Regional Settings (like discussed here), but I didn't realize any setup would make them not render on the spreadsheet since for me, most Unicode characters display properly on a worksheet.
I assume the characters don't work for you in other applications besides Excel. If that's not the case, and this problem is specific to Excel (or if you can't otherwise get it figured out by adjusting Windows Regional Settings), then I'd suggest contacting Microsoft Tech Support.
More information is here or here or in this search might also be helpful.
hi i had same problem in latvian
just uncheck utf - 8 beta in regional language settings for non unicode
enter image description here

Persian date format in TextBox Excel vba

I am trying to apply Persian date format in text boxes inside some worksheet.
The Text boxes now stores the date in format "dd/mm/yyyy hh:mm" And that's a thing that i applied by myself.
But i'm struggling with applying Persian date in these text boxes. I recorded some macro when i'm changing date format of cell to Persian, and i was trying to use it on text box but this code does not seem to work properly.
This is code that i'm using now and it is working properly
TextBox1 = Format(TextBox1, "dd/mm/yyyy hh:mm")
and those lines I was trying to use to apply Persian date but I don't get any effect
TextBox3 = Format(TextBox3, "[$-fa-IR,16]dd/mm/yyyy;#")
And this is the effect of recording macro and changing date format to Persian manually. From this line i got a formula and try to implement it above.
Range("J30").Select
Selection.numberFormat = "[$-fa-IR,16]dd/mm/yyyy;#"
Does any of you could provide me with some reasonable solution to this?
Thank you in advance for your Help.
This blog post should be of help to you as it covers a number of different date formats, including Persian https://www.jquery-az.com/3-ways-change-date-format-excel-14-date-formulas/
In excel, the following formula will format a standard date in Persian:
=TEXT(A1,"[$-0429]mmmm d,yyyy")
So in VBA, the following should work:
Range("J30").numberFormat = "[$-0429]mmmm d, yyyy"
In terms of formatting within a text box, providing the textbox text is referring to a cell location, formatting this cell location will impact on the format of the textbox - see illustration below.
If you are trying to format text directly within the textbox you might try adding .value to your code snippet above eg.
TextBox3.value = Format(TextBox3.value, "[$-fa-IR,16]dd/mm/yyyy;#")
I only have access to Office for Mac at work and that feature is not supported for me to test.
This blog post discusses how to apply formatting to text boxes via VBA but it may not support language formatting options. If thats the case you may consider amending your text boxes to reference a cell location that you can format.
https://www.extendoffice.com/documents/excel/4899-excel-format-textbox-as-currency.html
Have you installed Persian?
File > Options > Language > [Add additional editing languages] - Select Persian > Add > OK
You will probably need to restart Excel and may need to re-apply the formatting.

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

Does Range.Formula property translate to other locale

When a user manually enters formula in a sheet in the English version of Excel and sends this document to another user who has Excel in another version, the formula is automatically translated to the language in which the document is opened.
'Formula entered in Sheet1!B1 in the English Excel version
=VLOOKUP(A1,Sheet2!$A$1:$C$150,3,FALSE)
'Formula as shown in a Dutch Excel version
=VERT.ZOEKEN(A1;Sheet2!$A$1:$C$150;3;ONWAAR)
When I programmatically add a formula, in my English version, it would look like this:
Sub AddFormula()
ThisWorkbook.Worksheets("Sheet1").Cells(1, 2).Formula = "=VLOOKUP(A1,Sheet2!$A$1:$C$150,3,FALSE)"
End Sub
If I were to run this procedure on my English Version, then save the document and send it over to a user with another language, it would be fine as the formula is in the cell and thus translated to whatever language and locale the 2nd user uses.
My question is what happens when the 2nd user runs the procedure, in the Dutch (or other language for that matter) Excel with the formula in the code written in the EN-US format. Will it be translated or will it throw an error and in case of the latter, how would I solve that and make the formula insertion language proof?
In Excel vba the .Formula requires that the formula to be inserted be in EN-USA format. This is regardless of the local settings.
Excel will then translate it into the local vernacular when putting it in the sheet.
So no matter what the local settings it will work cross languages.
VBA also has the .FormulaLocal which allows the entry of the formula in the native language. But (as far as I know) it will not work cross different local settings.

Always format cells general to text in Excel

I am using Microsoft Excel 2010. In that after entering the values for example if i enter "01234" and give enter it changes to "1234". The "0" is getting eliminated. To solve this issue i ll do the following procedure
Right click on the cell --> format cells --> Number Tab --> choose Text in category column.
I hate this process to do often. Is there any solution to predefine it meaning it should always be in "text" whenever i open a new workbook.
You can refer to this link to do what you want:
http://www.pchelps.com/2011/03/productivity-101-setting-up-excel-default-formatting/
Basically, open a workbook and format it in the way you want. Then save that as your template.
An alternative to making your format Text is to use custom formatting. For example if you know that your number will always have 5 digits (sometimes with leading 0s), you can do a custom format with 00000.
In this way, even when you enter manually 123 into the cell, it will be formatted as 00123

Resources