Page 1 of 1

Increment

Posted: Fri Apr 27, 2018 10:41 pm
by garrowolf
I am trying to figure out how to increment the contents of a cell. I am going to assign it to a button so I can press one button and have several other cells increment and some decrement. I figured out how to create a macro but I can't find anything on this.

Re: Increment

Posted: Fri Apr 27, 2018 10:47 pm
by FJCC
This macro increases the value of Sheet1.A1 by one with each execution.

Code: Select all

oSheets = ThisComponent.Sheets
oSheet1 = oSheets.getByName("Sheet1")
oCellA1 = oSheet1.getCellrangeByName("A1")
ValueA1 = oCellA1.Value
oCellA1.Value = ValueA1 + 1

Re: Increment

Posted: Fri Apr 27, 2018 11:01 pm
by garrowolf
Thank you! Let me go try it out.

Re: Increment

Posted: Sat Apr 28, 2018 9:39 am
by Lupp
Concerning the original demand to increment / decrement more than one cells by different amounts in one go:

If you want to apply some incrementing / decrementing of cells by a Sub invoked by a click on an OK-button, you have a basic problem:

A) Either you hard-code the information which cells to treat in what way in the Sub itself OR
B) You find a way to pass that information to a Sub capable to interpret it as needed. This is not specifically supported.

Some viable ways to pass parameters as option B requires you find listed in my recent answer to this question in a different forum:
https://ask.libreoffice.org/en/question ... arameters/ .

All these ways suffer, however, from the need to create/apply a specific syntax for the parameter list, and to analyse it by a Sub.
Using the 'Tag' property 'Additional information' of a form control you can at least write the parameter list in a similar way as it would occur in a function call.

See the attached demonstration.

You need to check the included code to be sure it is safe, and to reload the file then permitting macro execution.
(Of course, you can also move the module to your standard library or create your own version there.)

Re: Increment

Posted: Sat Apr 28, 2018 3:29 pm
by Villeroy