简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM