Changing cell data on button click

Im using two buttons and assigning a recorded macro to them, the only way i could get this to work was by assigning the same macro to both mouse down and mouse up events otherwise a double click was required.

I created two macros, ToggleOn and ToggleOff both are essentially the same code with just one edit, here is the code:

REM  *****  BASIC  *****

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SelectObject", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "ToPoint"
args2(0).Value = "$C$4"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args2())

rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "StringName"
args3(0).Value = "n"

dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())

rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:JumpToNextCell", "", 0, Array())

end sub

The change i referred to is just a “y” or “n”.

args3(0).Value = "n"

Can anybody tell me is there a better way to do this, Im not fussy about the form control, button check box or otherwise, i would just prefer to have only one form control that toggles the cell data if possible.

Recorded “macros” are rarely of much use.
If I understood correctly you want to switch from “y” to “n” and back the content of a specific cell when clicking on an OK-button. To do this for cell C4 of the current sheet you may use the following code:

option explicit

sub toggle_yn_Xis2_Yis3()	
' Internally X (left to right) and Y (top down) are starting with (X=0,Y=0) for A1.
	dim theDoc as Object, theSheet as Object, theCellC4 as Object
theDoc = ThisComponent
theSheet = theDoc.Sheets(theDoc.GetCurrentSelection.RangeAddress.Sheet)
theCellC4 = theSheet.GetCellByPosition(2, 3)
select case theCellC4.String
	case "y": theCellC4.String = "n"
	case "n": theCellC4.String = "y"
end select
end sub ..

If C4 is containing something else but “y” or “n”, no action will be taken.
See attached: ask63443ToggleSpecificCellBySub001.ods
The included sub is assigned to the event ‘MouseButtonReleased’ for the button showing “y/n”

===Edit 2020-08-29 about 13:45 UTC===
During the last 4.5 (+) years I gained a bit of additional experience, and I changed my mind with some respects.
In the specific case of toggling a cell content, I would now suggest to use a CheckBox control and no usercode at all.
If usercode should be called by clicking a Button control, the event listed as ‘Execute action’ in the object editor should be used.