Hello,
with help from online resources I wrote my first macro. It is a function which converts the currency format from a given cell into its textual representation, i.e. if the currency in a given cell is “$”, it will output “USD”, if it is “€”, it will output “EUR”. I am using the text for another function, which will fetch exchange rates from ECB.
Example usage: Let’s have the numeric value of “19” formatted as currency in € in cell A1, I can use the formula “=CURRENCYABBREV(A1)” in cell A2 and it gives me “EUR”.
The function works well when I insert it into the sheet. I can save the sheet and exit, but upon reopening the file, I get an error message “BASIC Runtime error 423 Numberformats”. If I click OK the error disappears, I can see the sheet but the cell A2 is empty. This error message appears as many times as I have cells where I am using the function.
The strange thing is, if I fill in the function again, the result appears in the cell.
Maybe I am missing some other basics of using macros here, because obviously the function itself is working.
This is the definition of CURRENCYABBREV:
Option VBAsupport 1
Function CurrencyAbbrev (sAddress As Variant) As String
Dim oDoc as Object: oDoc = ThisComponent
Dim oNumberFormats as Variant: oNumberFormats = oDoc.Numberformats
Dim nRow As Long: nRow = sAddress.Row - 1
Dim nCol As Long: nCol = sAddress.Column - 1
Dim oSheet As Variant: oSheet = ThisComponent.CurrentController.ActiveSheet
Dim oCell As Variant: oCell = oSheet.getCellByPosition(nCol,nRow)
Dim nFormatCodeKey as Long: nFormatCodeKey = oCell.NumberFormat
Dim oFormat as Variant: oFormat = oNumberFormats.getByKey(nFormatCodeKey)
CurrencyAbbrev = oFormat.CurrencyAbbreviation
End Function
Any help is appreciated. I am using LibreOffice 6.4.7.2 on Ubuntu 20.04.
CurrencyAbbrevTest.ods (9.3 KB)