Bug 162067 - VBA Macro changes cell background to incorrect colour
Summary: VBA Macro changes cell background to incorrect colour
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
3.5.0 release
Hardware: All All
: medium minor
Assignee: Not Assigned
URL:
Whiteboard:
Keywords: filter:xlsx
Depends on:
Blocks: Macro-VBA
  Show dependency treegraph
 
Reported: 2024-07-17 07:43 UTC by KosulinLeonidas
Modified: 2024-12-21 15:46 UTC (History)
2 users (show)

See Also:
Crash report or crash signature:


Attachments
Demo file (16.34 KB, application/vnd.oasis.opendocument.spreadsheet)
2024-07-17 07:43 UTC, KosulinLeonidas
Details
Minimal xlsm Document (10.87 KB, application/vnd.ms-excel.sheet.macroEnabled.12)
2024-09-04 08:47 UTC, KosulinLeonidas
Details
xlsm with libreoffice macro (11.48 KB, application/vnd.ms-excel.sheet.macroEnabled.12)
2024-09-04 08:52 UTC, KosulinLeonidas
Details

Note You need to log in before you can comment on or make changes to this bug.
Description KosulinLeonidas 2024-07-17 07:43:17 UTC
Description:
When modifying and saving any code in my LibreOffice Document, changing the background color of a cell using RGB values results in the cell displaying an incorrect color. In my example, setting a cell's background color to RGB(0, 0, 255) turns it blue as expected, but after changing or commenting out any line of code and saving, the cell's background color changes to red instead of the specified blue. Calculating the value myself, so using just 255 instead of RGB(0,0,255) in this scenario is a working workaround

Steps to Reproduce:
1. Open ColorDemo document
2. Run SetCellColor function
3. Observe result (cell turns blue)
4. Comment out the SetSheetRef function and save
5. Run SetCellColor again
6. Observe difference in results

Actual Results:
After saving the change in the macro code, the Subroutine starts to turn cell A1 Red

Expected Results:
Sub should continue to turn cell Blue


Reproducible: Always


User Profile Reset: Yes

Additional Info:
Version: 24.2.4.2 (X86_64) / LibreOffice Community
Build ID: 51a6219feb6075d9a4c46691dcfe0cd9c4fff3c2
CPU threads: 16; OS: Windows 10.0 Build 19045; UI render: Skia/Vulkan; VCL: win
Locale: de-DE (de_DE); UI: en-US
Calc: CL threaded
Comment 1 KosulinLeonidas 2024-07-17 07:43:51 UTC
Created attachment 195346 [details]
Demo file
Comment 2 Buovjaga 2024-09-03 08:16:58 UTC
SetCellColor and SetSheetRef functions reside in BACnet_Template_Makros - Modules - SC_RS_Modules

I reproduce with the file all the way to 3.5.0, no need to save in between, just run SetCellColor immediately after commenting out SetSheetRef.

However, I can not reproduce in a new file. So I think something in your macros is interfering with this. As I spent already quite a lot of time on this, I would appreciate if you investigated it further to find out what in your collection of macros is the reason for this.
Comment 3 KosulinLeonidas 2024-09-04 07:38:16 UTC
(In reply to Buovjaga from comment #2)
> SetCellColor and SetSheetRef functions reside in BACnet_Template_Makros -
> Modules - SC_RS_Modules
> 
> I reproduce with the file all the way to 3.5.0, no need to save in between,
> just run SetCellColor immediately after commenting out SetSheetRef.
> 
> However, I can not reproduce in a new file. So I think something in your
> macros is interfering with this. As I spent already quite a lot of time on
> this, I would appreciate if you investigated it further to find out what in
> your collection of macros is the reason for this.

I just managed to reproduce the bug.
As it turns out, the problem is opening an excel xlsm document with libreoffice and saving it as .ods

It should be possible to reproduce by doing the following:

1. Create a new Excel Document
2. Record a Macro (I just changed A1 to blue)
3. Save as .xlsm
4. Open document with Libreoffice Calc (Right click -> Open with -> Libreoffice)
5. Replace the macro with the libreoffice equivalent

And now the Bug will happen. If you open the document and run it, the cell turns blue. If you change anything in the code (e.g. add a space at the end of the comment), the macro starts turning the cell red. 

I did not manage to reverse this issue. Deleting the module and creating a new one in the standard location did nothing.

For reference, here is the macro:


'Sets the Background of Cell A1 to Blue
'it will start turning cell A1 to Red after modifying the code anyhow. Even adding a space at the end of this comment does the trick (no saving needed) 
Sub SetCellColor
	oSheet = ThisComponent.CurrentController.ActiveSheet
	oSheet.getCellByPosition(0,0).CellBackColor = RGB(0, 0, 255)
End sub


As a Workaround I wrote this Macro:

Function RGBToVal(r As Integer, g As Integer, b As Integer) As Long
    RGBToVal = (r * 65536) + (g * 256) + b
End Function

And Replaced "RGB(" with "RGBToVal(" using Ctrl+H.

Let me know if you need any more info
Comment 4 Buovjaga 2024-09-04 07:44:42 UTC
(In reply to KosulinLeonidas from comment #3)
> I just managed to reproduce the bug.
> As it turns out, the problem is opening an excel xlsm document with
> libreoffice and saving it as .ods

Can you attach such a minimal xlsm document? Thanks.
Comment 5 KosulinLeonidas 2024-09-04 08:47:13 UTC
Created attachment 196217 [details]
Minimal xlsm Document

1. Open Minimal xlsm Doc
2. Paste following macro into module1
3. Macro turns cell a1 red instead of blue


Sub SetCellColor
	oSheet = ThisComponent.CurrentController.ActiveSheet
	oSheet.getCellByPosition(0,0).CellBackColor = RGB(0, 0, 255)
End sub
Comment 6 KosulinLeonidas 2024-09-04 08:49:40 UTC
(In reply to Buovjaga from comment #4)
> (In reply to KosulinLeonidas from comment #3)
> > I just managed to reproduce the bug.
> > As it turns out, the problem is opening an excel xlsm document with
> > libreoffice and saving it as .ods
> 
> Can you attach such a minimal xlsm document? Thanks.

It seems like saving it as .ods isn't even necessary, nor is it for the xlsm doc to have any macros in it.

I recorded a macro just for it to create a module and deleted it right after. It seems like opening the xlsm document with libreoffice breaks something but I can't figure out why.

Just opening the document with libreoffice, pasting the other macro and running it already reproduces the bug
Comment 7 KosulinLeonidas 2024-09-04 08:52:22 UTC
Created attachment 196218 [details]
xlsm with libreoffice macro

This one you can just open with libreoffice and instantly run the macro. It will turn the cell red instead of blue. No need to change anything at all
Comment 8 Buovjaga 2024-09-04 09:21:05 UTC
(In reply to KosulinLeonidas from comment #7)
> Created attachment 196218 [details]
> xlsm with libreoffice macro
> 
> This one you can just open with libreoffice and instantly run the macro. It
> will turn the cell red instead of blue. No need to change anything at all

Thanks. It's in VBAProject - Modules. Already seen in 3.5.0.