Hello everybody,
My macro practice has been a long time ago. Can someone please tell me the source code for the following example (The rest I should create myself, but the beginning is very difficult for me, which classes / objects / variables such as declare / define, address lines etc ...)?
I would like to add the numbers from column B to column D by pressing the button.
After pressing the button twice, the upper result would come out. After three times:
3
6
9
etc...
Many, many thanks in advance !!!
Andrej
Calculating by macro
Calculating by macro
Apache OpenOffice 4.1.5
Win10 Pro, 10.0.17134
Win10 Pro, 10.0.17134
Re: Calculating by macro
If there are only three cells as in your example, you can use this
If there are many cells, a more elegant solutions would use a loop.
Code: Select all
oSheet1 = ThisComponent.Sheets.getByName("Sheet1")
oCellB2 = oSheet1.getCellrangeByName("B2")
oCellB3 = oSheet1.getCellrangeByName("B3")
oCellB4 = oSheet1.getCellrangeByName("B4")
oCellD2 = oSheet1.getCellrangeByName("D2")
oCellD3 = oSheet1.getCellrangeByName("D3")
oCellD4 = oSheet1.getCellrangeByName("D4")
oCellD2.Value = oCellD2.Value + oCellB2.Value
oCellD3.Value = oCellD3.Value + oCellB3.Value
oCellD4.Value = oCellD4.Value + oCellB4.Value
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Calculating by macro
Als erstes vielen Dank für die schnelle Antwort!
Habe noch eine weiter Frage im Rahmen meines „Projekts“:
Ich lasse Zufallswerte in mehreren Zellen berechnen (via „Shift+Strg+F9“) solange bis deren Summe („B2“) eine max. Abweichung von 5% vom Basiswert ergibt.
Ich habe ein Makro aufgezeichnet und versucht eine Schleife um diese Aufzeichnung herumzubasteln, komme allerdings nicht weiter... ===
sub Zufall
dim document as object
dim dispatcher as object
Dim oDoc as Object
Dim oSheet as Object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDoc=ThisComponent
oSheet=oDoc.Sheets().getByIndex(0)
oCellA2 = oSheet.getCellrangeByName("A2")
oCellB2 = oSheet.getCellrangeByName("B2")
dispatcher.executeDispatch(document, ".uno:CalculateHard", "", 0, Array())
While ((oCellA2.value / oCellB2.value)< (0,95 * oCellA2.value) OR (oCellA2.value / oCellB2.value)> (1,05 * oCellA2.value))
dispatcher.executeDispatch(document, ".uno:CalculateHard", "", 0, Array())
Wend
end sub
===
Kann mir da bitte jemand helfen?
Vielen Dank!!!
Habe noch eine weiter Frage im Rahmen meines „Projekts“:
Ich lasse Zufallswerte in mehreren Zellen berechnen (via „Shift+Strg+F9“) solange bis deren Summe („B2“) eine max. Abweichung von 5% vom Basiswert ergibt.
Ich habe ein Makro aufgezeichnet und versucht eine Schleife um diese Aufzeichnung herumzubasteln, komme allerdings nicht weiter... ===
sub Zufall
dim document as object
dim dispatcher as object
Dim oDoc as Object
Dim oSheet as Object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
oDoc=ThisComponent
oSheet=oDoc.Sheets().getByIndex(0)
oCellA2 = oSheet.getCellrangeByName("A2")
oCellB2 = oSheet.getCellrangeByName("B2")
dispatcher.executeDispatch(document, ".uno:CalculateHard", "", 0, Array())
While ((oCellA2.value / oCellB2.value)< (0,95 * oCellA2.value) OR (oCellA2.value / oCellB2.value)> (1,05 * oCellA2.value))
dispatcher.executeDispatch(document, ".uno:CalculateHard", "", 0, Array())
Wend
end sub
===
Kann mir da bitte jemand helfen?
Vielen Dank!!!
Apache OpenOffice 4.1.5
Win10 Pro, 10.0.17134
Win10 Pro, 10.0.17134
Re: Calculating by macro
Does the attached file do what you want?
- Attachments
-
- SumRandom.ods
- (9.22 KiB) Downloaded 135 times
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
- Hagar Delest
- Moderator
- Posts: 32650
- Joined: Sun Oct 07, 2007 9:07 pm
- Location: France
Re: Calculating by macro
Thanks to keep in English. Your discussion may be useful to other English speaking users.Andrej wrote:Als erstes vielen Dank für die schnelle Antwort!
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10