Bug 52602 - Excel Visual Basic compatibility issue in LibreOffice Calc: NumberFormat to date/time
Summary: Excel Visual Basic compatibility issue in LibreOffice Calc: NumberFormat to d...
Status: VERIFIED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
3.5.5.3 release
Hardware: All All
: medium normal
Assignee: Eike Rathke
URL:
Whiteboard: target:7.5.0 target:7.4.0.0.beta2
Keywords:
Depends on:
Blocks: Macro-VBA
  Show dependency treegraph
 
Reported: 2012-07-27 19:51 UTC by Tor24_1975314
Modified: 2022-06-20 02:27 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:


Attachments
Excel test file with macro (38.00 KB, application/vnd.ms-excel)
2012-07-27 19:51 UTC, Tor24_1975314
Details
Excel_VBA_Test_Working.xls (11.00 KB, application/vnd.ms-excel)
2022-06-13 04:19 UTC, Óvári
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tor24_1975314 2012-07-27 19:51:40 UTC
Created attachment 64804 [details]
Excel test file with macro

Excel Visual Basic Date/time formatting does not work, Excel macro code examples:

Example macro:

Sub Test_NumberFormat_DateTime()
    ' Preparing Test:
    Range("A3").FormulaR1C1 = "40969.6388888889"
    Range("B3").FormulaR1C1 = "40969.6388888889"
    Range("C3").FormulaR1C1 = "40969.6388888889"
    Range("D3").FormulaR1C1 = "40969.6388888889"
    Range("E3").FormulaR1C1 = "40969.6388888889"
    Range("F3").FormulaR1C1 = "40969.6388888889"
    Range("A3").Select

    ' Testing Excel VBA Code in Libre Office:
    Range("A3").NumberFormat = "hh:mm"
    Range("B3").NumberFormat = "mm/dd/yyyy hh:mm"
    Range("C3").NumberFormat = "mm/dd/yyyy hh:mm:ss"
    Range("D3").NumberFormat = "d/m/yy h:mm"
    Range("E3").NumberFormat = "d/ mmmm yyyy"
    Range("F3").NumberFormat = "d/ mmm yyyy"
End Sub


The same problem with the NumberFormat function on Windows systems with other regional settings.
Example Windows "Regional and Language Options": "German (Germany)" 
with Formula/List Separator ';' and Decimal Symbol ','

Sub Test_NumberFormat_Number()
    Range("A3").NumberFormat = "0,00000000"
    Range("B3").NumberFormat = "0.00000000"
    Range("C3").NumberFormat = "#,##0.00000"
End Sub
Comment 1 Tor24_1975314 2012-09-02 11:45:16 UTC
This bug occurs in LibreOffice version 3.5.6 and 3.6.1
Comment 2 Julien Nabet 2014-02-09 15:53:49 UTC
Tor: do you reproduce this with a newer LO version (4.1.4 or 4.2.0.4)?
Comment 3 Tor24_1975314 2014-02-18 18:36:51 UTC
Yes, I reproduced this bug also in newer versions of LibreOffice for Windows! 

Tested with LibreOffice versions: 4.0.4.2, 4.1.4.2 and 4.2.0.4
Comment 4 Tor24_1975314 2014-05-04 14:47:04 UTC
This bug was reproduced also in LibreOffice Calc version 4.2.3.3 and 4.2.4.1 rc!
Comment 5 QA Administrators 2015-06-08 14:42:52 UTC Comment hidden (obsolete)
Comment 6 Tor24_1975314 2015-06-15 21:36:08 UTC
The problem still exists!

This bug was also reproduced in LibreOffice 4.4.3.2!
Comment 7 Tor24_1975314 2016-06-26 14:37:49 UTC
This problem still exists and was also reproduced in LibreOffice versions 4.4.7.2, 5.0.4.2, 5.1.1.3 and 5.1.4.2 for Windows.

(Test system: Windows 7 64-bit, Intel Core2Duo P8700 2.53 Ghz, 4 GB RAM, Java SE Runtime Environment build 1.6.0_24-b07)
Comment 8 Xisco Faulí 2017-06-12 11:53:20 UTC
Changing version back to the earliest affected version.
Comment 9 Tor24_1975314 2017-11-05 20:43:22 UTC
Bug also reproduced with the newest version of LibreOffice 5.4.2 for Windows.
Comment 10 QA Administrators 2019-07-30 03:16:46 UTC Comment hidden (obsolete)
Comment 11 QA Administrators 2021-07-30 06:22:29 UTC Comment hidden (obsolete)
Comment 12 Óvári 2022-06-12 10:11:05 UTC
The problem still exists in LibreOffice 7.3.4.2 with Linux Mint 20.3 Cinnamon.

Another variations on the Number format which don't exist are shown below.

Sheets(1).Columns(1).NumberFormat = "@"
Sheets(1).Columns(2).NumberFormat = "$#,##0.00;[Red]$#,##0.00"
Sheets(1).Columns(3).NumberFormat = "$#,##0.00"" Cr"";[Red]$#,##0.00"" Dr"""

Above code modified from Statement.xls attachment in bug 149325.

Thank you
Comment 13 Óvári 2022-06-13 04:19:50 UTC
Created attachment 180703 [details]
Excel_VBA_Test_Working.xls

It seems that LibreOffice Calc Macros are case-sensitive.

Change:
d → D
m → M
y → Y
h → H
m → M
s → S

Does it work for you?

Thank you

(In reply to Tor24_1975314 from comment #0)
Modified Excel test file with macro

Modified Excel Visual Basic Date/time formatting to make it work, Excel macro code examples:

Example macro:

Sub Test_NumberFormat_DateTime()
    ' Preparing Test:
    Range("A3").FormulaR1C1 = "40969.6388888889"
    Range("B3").FormulaR1C1 = "40969.6388888889"
    Range("C3").FormulaR1C1 = "40969.6388888889"
    Range("D3").FormulaR1C1 = "40969.6388888889"
    Range("E3").FormulaR1C1 = "40969.6388888889"
    Range("F3").FormulaR1C1 = "40969.6388888889"
    Range("A3").Select

    ' Testing Excel VBA Code in Libre Office:
    Range("A3").NumberFormat = "HH:MM"
    Range("B3").NumberFormat = "MM/DD/YYYY HH:MM"
    Range("C3").NumberFormat = "MM/DD/YYYY HH:MM:SS"
    Range("D3").NumberFormat = "D/M/YY H:MM"
    Range("E3").NumberFormat = "D/ MMMM YYYY"
    Range("F3").NumberFormat = "D/ MMM YYYY"
End Sub
Comment 14 Óvári 2022-06-13 04:25:20 UTC
It seems that color (e.g. RED) is case-sensitive and the currency symbol needs quotes to make it a string (e.g. "$")

(In reply to Óvári from comment #12)

Microsoft Excel                      →   LibreOffice Calc
dd mmm yyyy                          →   DD MMM YYYY
$#,##0.00;[Red]$#,##0.00             →   $#,##0.00;[RED]"$"#,##0.00
$#,##0.00" Cr";[Red]$#,##0.00" Dr"   →   $#,##0.00" Cr";[RED]"$"#,##0.00" Dr"
Comment 15 Eike Rathke 2022-06-14 10:01:26 UTC
It's only the dreaded API that the VBA layer uses that makes it case-sensitive.

The effect of making "$" a quoted string is none, or just that the format code doesn't exist yet and can be added without duplicate (where case-insensitive duplicates effectively prevent the assignment to NumberFormat from working). The actual difference in the example is [RED] vs [Red]
Comment 16 Commit Notification 2022-06-15 09:05:11 UTC
Eike Rathke committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/8661d5579bd19a9e294ddff64bbe817b537dbd46

Resolves: tdf#149325 tdf#52602 SvNumberFormatsObj::queryKey try also uppercase

It will be available in 7.5.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 17 Commit Notification 2022-06-15 12:07:18 UTC
Eike Rathke committed a patch related to this issue.
It has been pushed to "libreoffice-7-4":

https://git.libreoffice.org/core/commit/c98a60742a25eb4d4526f109f9af9de391da0643

Resolves: tdf#149325 tdf#52602 SvNumberFormatsObj::queryKey try also uppercase

It will be available in 7.4.0.0.beta2.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 18 Commit Notification 2022-06-15 20:45:19 UTC
Xisco Fauli committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/fc5357935cc160fd19b61db7cbf7f098962d42a3

tdf#52602: sc_vba_macro_test: Add unittest

It will be available in 7.5.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 19 Commit Notification 2022-06-16 09:23:03 UTC
Eike Rathke committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/a4fa06be4f8cd4d2584a6f3e4c5bd02786e591ad

Related: tdf#149325 tdf#52602 SvNumberFormatsObj::addNew accept differing

It will be available in 7.5.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 20 Commit Notification 2022-06-16 12:54:47 UTC
Eike Rathke committed a patch related to this issue.
It has been pushed to "libreoffice-7-4":

https://git.libreoffice.org/core/commit/e5f403b7e6a5d2d99a01a2111224b5277beba0d5

Related: tdf#149325 tdf#52602 SvNumberFormatsObj::addNew accept differing

It will be available in 7.4.0.0.beta2.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 21 Óvári 2022-06-20 02:27:18 UTC
Verified fixed with LibreOffice
Version: 7.5.0.0.alpha0+ / LibreOffice Community
Build ID: ca47989ad60b1414f92be22a1fbf4c1d1a92dd97
CPU threads: 2; OS: Linux 5.4; UI render: default; VCL: gtk3
Locale: en-AU (en_AU.UTF-8); UI: en-US
Calc: threaded

with Linux Mint 20.3 Cinnamon

Thank you