Ask LibreOffice - RSS feedhttps://ask.libreoffice.org/en/questions/Questions and answers for LibreOfficeenMon, 19 Oct 2015 23:18:43 +0200How can do function like follows in Libre Officehttps://ask.libreoffice.org/en/question/59712/how-can-do-function-like-follows-in-libre-office/I'm new with this Basic language. I have a Java back round and I can't run the following Function. How do I need to setup it to run in LibreOffice. Apparently Function can not be called inside functions LibreOffice Basic?
REM ***** BASIC *****
Sub Main
End Sub
Function pPeriod(months, qLed, qHalo, lifeLed, lifeHalo, hoursD, pLed, pHalo, tranFee, ePrice)
For iCount = 0 To months
newLeds = ROUNDDOWN((iCount*hoursD*30/lifeLed);1)
newHalos = ROUNDDOWN((iCount*hoursD*30/lifeHalo);1)
qLed = qLed + newLeds
qHalo = qHalo + newHalos
newPriceLeds = qLed*pLed + tranFee + 30*hoursD*ePrice
newPriceHalos = qHalo*pHalo + tranfee + 30*hoursD*ePrice
If newPriceLeds > newPriceHalos = True Then Exit For
Print i
End FunctionMon, 19 Oct 2015 15:16:01 +0200https://ask.libreoffice.org/en/question/59712/how-can-do-function-like-follows-in-libre-office/Answer by Regina for <p>I'm new with this Basic language. I have a Java back round and I can't run the following Function. How do I need to setup it to run in LibreOffice. Apparently Function can not be called inside functions LibreOffice Basic?</p>
<p>REM <strong><em></em></strong><em> BASIC <strong></strong></em></p>
<p>Sub Main</p>
<p>End Sub</p>
<p>Function pPeriod(months, qLed, qHalo, lifeLed, lifeHalo, hoursD, pLed, pHalo, tranFee, ePrice)</p>
<pre><code>For iCount = 0 To months
newLeds = ROUNDDOWN((iCount*hoursD*30/lifeLed);1)
newHalos = ROUNDDOWN((iCount*hoursD*30/lifeHalo);1)
qLed = qLed + newLeds
qHalo = qHalo + newHalos
newPriceLeds = qLed*pLed + tranFee + 30*hoursD*ePrice
newPriceHalos = qHalo*pHalo + tranfee + 30*hoursD*ePrice
If newPriceLeds > newPriceHalos = True Then Exit For
Print i
</code></pre>
<p>End Function</p>
https://ask.libreoffice.org/en/question/59712/how-can-do-function-like-follows-in-libre-office/?answer=59728#post-id-59728For to use Calc functions inside Basic see [this answer](https://ask.libreoffice.org/en/question/28048/can-we-use-regular-spreadsheet-functions-in-basic-functions/) .
For a general guide to Basic use [Andrew Pitonyak: OpenOffice.org Macros Explained](http://www.pitonyak.org/AndrewMacro.odt).
Your example is missing a `Next iCount`.Mon, 19 Oct 2015 20:26:28 +0200https://ask.libreoffice.org/en/question/59712/how-can-do-function-like-follows-in-libre-office/?answer=59728#post-id-59728Answer by Lupp for <p>I'm new with this Basic language. I have a Java back round and I can't run the following Function. How do I need to setup it to run in LibreOffice. Apparently Function can not be called inside functions LibreOffice Basic?</p>
<p>REM <strong><em></em></strong><em> BASIC <strong></strong></em></p>
<p>Sub Main</p>
<p>End Sub</p>
<p>Function pPeriod(months, qLed, qHalo, lifeLed, lifeHalo, hoursD, pLed, pHalo, tranFee, ePrice)</p>
<pre><code>For iCount = 0 To months
newLeds = ROUNDDOWN((iCount*hoursD*30/lifeLed);1)
newHalos = ROUNDDOWN((iCount*hoursD*30/lifeHalo);1)
qLed = qLed + newLeds
qHalo = qHalo + newHalos
newPriceLeds = qLed*pLed + tranFee + 30*hoursD*ePrice
newPriceHalos = qHalo*pHalo + tranfee + 30*hoursD*ePrice
If newPriceLeds > newPriceHalos = True Then Exit For
Print i
</code></pre>
<p>End Function</p>
https://ask.libreoffice.org/en/question/59712/how-can-do-function-like-follows-in-libre-office/?answer=59737#post-id-59737[ROUNDDOWN() is a function of doubtable justification. It actually does not round down the value it got, but rounds down the absolute amount of it and then glues the screwed off sign back to the result.]
In BASIC you will not need a Calc function called via a FunctionAccess object in the above situation but simply a replacement of ROUNDDOWN using a BASIC function doing the same thing if applied to a positive value - as you will have - in the following way:
newLeds = Int((iCount*hoursD*30/lifeLed)*10)/10
[If you actually had negative values and wanted them treated as ROUNDDOWN would do, you had to use the BASIC function 'Fix' instead of 'Int'.]
Mon, 19 Oct 2015 23:18:43 +0200https://ask.libreoffice.org/en/question/59712/how-can-do-function-like-follows-in-libre-office/?answer=59737#post-id-59737