Example of converting VBA macro

This VBA macro is as simple as I can make it:

Option Explicit

Private Sub CommandButton1_Click()

Range(“E8,E9”).Value = “0”

End Sub

Would someone please convert this so that it will run in LO?

I just need an example of how it would be coded.

Edit: In a convoluted way (Tools|Macros …Run)I have been able to determine that the supplied code works as it should. However I cannot figure out how to get it on my sheet so that when I click the “box” the macro does it’s thing. (Realize I have been using LO calc for all of two hours.) What Form Control needs to be placed on the sheet? The only one not greyed out is Pushbutton. I have it on my sheet but don’t know what to do with it!

In summary, I have the code and the sheet–what I need is instructions on how to activate the code by pressing a macro 'button" and where to put the code. I appreciate your coding help Ratslinger.

Sub CommandButton1_Click()
	Dim my_data(1,0)
	my_data(0,0) = 0
	my_data(1,0) = 0
	my_range = ThisComponent.Sheets(0).getCellRangebyName("E8:E9")
	my_range.setDataArray(my_data)	
End Sub

This is provided the sheet wanted is the first sheet. You can also get the sheet by name:

my_range = ThisComponent.Sheets.getByName("SHEET_NAME").getCellRangebyName("E8:E9")

For much more on macros for Calc (and other sections) see OOME (Open Office Macros Explained by Andrew Pitonyak) pdf - click here

Edit:

To connect pushbutton to macro:

First get form design toolbar displayed. From menu View->Toolbars1 and make sure Form Design is checked. On Form Design toolbar enter Design Mode by clicking on the icon (second from left). Now right click on your pushbutton and from list select Control. This brings up properties for the control.

Select the Events tab. Here you will use the Execute action event. Click on the ellipsis on the far right of the event line. Assign Action macro dialog appears. Click on Macro... button.

Locate where you have placed the macro. When found, click on the name of the macro & then click the OK button. You are back on the dialog so click OK here. Close the properties box. Turn off Design Mode on the toolbar. General view of parts discussed:

image description

Your pushbutton should now execute your macro.

You should really go through the LO documentation for Calc (Chapter 12 Macros) and OOME for further details of macro libraries. LibreOffice documentation - click here.

If this answers your question please click on the :heavy_check_mark: (upper left area of answer).