VBASupport1b.ods is the original file.
I added Option VBASupport 1
, used Round
function and saved as VBASupport1a.ods.
After running Sub TransferValueContentFromCellToGrid
on VBASupport1a.ods, I got an error message:
BASIC syntax error.
Expected: =.
What is wrong in line 9 ?
VBASupport1a.ods (128.5 KB)
VBASupport1b.ods (13.1 KB)
LibreOffice:
Version: 7.3.4.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.4-0ubuntu0.22.04.1
Calc: threaded
OS:
Ubuntu 22.04 LTS
'VBASupport1a.ods
REM ***** BASIC *****
Option Explicit
Option VBASupport 1
Sub TransferValueContentFromCellToGrid
Dim gGrid()
ReDim gGrid(1 To 1)
ThisComponent.CurrentController.ActiveSheet.getCellByPosition(0, 0).setValue(3.1459)
MakeValueContentForGridSaveTemplate(0, 0, "m", gGrid(1), "s")
gGrid(1) = Round(gGrid(1), 2)
ThisComponent.CurrentController.ActiveSheet.getCellByPosition(0, 1).setValue(gGrid(1))
End Sub
Sub MakeValueContentForGridSaveTemplate(pCellPositionColumn&, pCellPositionRow&, pMandatoryOrNonMandatory$, pArrayElement, pIfIncorrectThenExitSubOrStop$)
Dim JumpToNextRow$ 'x
' Reset variable : JumpToNextRow
JumpToNextRow = ""
Dim oSheet As Object : oSheet = ThisComponent.CurrentController.ActiveSheet
If UCase(pMandatoryOrNonMandatory) = "MANDATORY" Or UCase(Left(pMandatoryOrNonMandatory,1)) = "M" Then
Select Case CheckCellContentType(pCellPositionColumn, pCellPositionRow) '[0=EMPTY/BLANK][1=VALUE/DATE][2=TEXT][3=FORMULA]
Case 1
' Set cell format to VALUE for sure.
ApplyNumberFormatToRange(pCellPositionColumn, pCellPositionRow,pCellPositionColumn, pCellPositionRow, "n")
pArrayElement = oSheet.getCellByPosition(pCellPositionColumn, pCellPositionRow).String
Case Else
If UCase(pIfIncorrectThenExitSubOrStop) = "E" Then
JumpToNextRow = "Y"
GoTo ExitThisSub
End If
If UCase(pIfIncorrectThenExitSubOrStop) = "S" Then
Msgbox "Incorrect data in cell >> " & ColumnNameOfColumnIndex(pCellPositionColumn) & pCellPositionRow + 1, ,"Error"
Stop
End If
End Select
ElseIf UCase(pMandatoryOrNonMandatory) = "NONMANDATORY" Or UCase(Left(pMandatoryOrNonMandatory,1)) = "N" Then
Select Case CheckCellContentType(pCellPositionColumn, pCellPositionRow) '[0=EMPTY/BLANK][1=VALUE/DATE][2=TEXT][3=FORMULA]
Case 0
pArrayElement = oSheet.getCellByPosition(pCellPositionColumn, pCellPositionRow).String 'Get blank as NULL
Case 1
' Set cell format to VALUE for sure.
ApplyNumberFormatToRange(pCellPositionColumn, pCellPositionRow, pCellPositionColumn, pCellPositionRow, "v")
pArrayElement = oSheet.getCellByPosition(pCellPositionColumn, pCellPositionRow).String
Case 2, 3
If UCase(pIfIncorrectThenExitSubOrStop) = "E" Then
JumpToNextRow = "Y"
GoTo ExitThisSub
End If
If UCase(pIfIncorrectThenExitSubOrStop) = "S" Then
Msgbox "Incorrect data in cell >> " & ColumnNameOfColumnIndex(pCellPositionColumn) & pCellPositionRow + 1, ,"Error"
Stop
End If
End Select
End If
ExitThisSub:
End Sub
Function CheckCellContentType%(pColumn&, pRow&)
Dim oDoc, oSheet, oCell As Object
oDoc = ThisComponent
oSheet = oDoc.getcurrentcontroller.activesheet
oCell = oSheet.getCellByPosition(pColumn, pRow)
Select Case oCell.Type
Case com.sun.star.table.CellContentType.EMPTY '0
CheckCellContentType = oCell.Type
Case com.sun.star.table.CellContentType.VALUE '1
CheckCellContentType = oCell.Type
Case com.sun.star.table.CellContentType.TEXT '2
CheckCellContentType = oCell.Type
Case com.sun.star.table.CellContentType.FORMULA '3
CheckCellContentType = oCell.Type
End Select
End Function
Function ApplyNumberFormatToRange(pStartColumn&, pStartRow&, pEndColumn&, pEndRow&, pDateNumberText$) As Variant
Dim oFormats As Object : oFormats = ThisComponent.NumberFormats
Dim oLocale As New com.sun.star.lang.Locale
Dim oRange As Object : oRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(pStartColumn, pStartRow, pEndColumn, pEndRow)
If UCase(pDateNumberText) = "D" Then : oRange.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.DATE, oLocale) : End If
If UCase(pDateNumberText) = "N" Then : oRange.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.NUMBER, oLocale) : End If
If UCase(pDateNumberText) = "T" Then : oRange.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.TEXT, oLocale) : End If
End Function
'VBASupport1b.ods
REM ***** BASIC *****
Option Explicit
'Option VBASupport 1
Sub TransferValueContentFromCellToGrid
Dim gGrid()
ReDim gGrid(1 To 1)
ThisComponent.CurrentController.ActiveSheet.getCellByPosition(0, 0).setValue(3.1459)
MakeValueContentForGridSaveTemplate(0, 0, "m", gGrid(1), "s")
' gGrid(1) = Round(gGrid(1), 2)
ThisComponent.CurrentController.ActiveSheet.getCellByPosition(0, 1).setValue(gGrid(1))
End Sub
Sub MakeValueContentForGridSaveTemplate(pCellPositionColumn&, pCellPositionRow&, pMandatoryOrNonMandatory$, pArrayElement, pIfIncorrectThenExitSubOrStop$)
Dim JumpToNextRow$ 'x
' Reset variable : JumpToNextRow
JumpToNextRow = ""
Dim oSheet As Object : oSheet = ThisComponent.CurrentController.ActiveSheet
If UCase(pMandatoryOrNonMandatory) = "MANDATORY" Or UCase(Left(pMandatoryOrNonMandatory,1)) = "M" Then
Select Case CheckCellContentType(pCellPositionColumn, pCellPositionRow) '[0=EMPTY/BLANK][1=VALUE/DATE][2=TEXT][3=FORMULA]
Case 1
' Set cell format to VALUE for sure.
ApplyNumberFormatToRange(pCellPositionColumn, pCellPositionRow,pCellPositionColumn, pCellPositionRow, "n")
pArrayElement = oSheet.getCellByPosition(pCellPositionColumn, pCellPositionRow).String
Case Else
If UCase(pIfIncorrectThenExitSubOrStop) = "E" Then
JumpToNextRow = "Y"
GoTo ExitThisSub
End If
If UCase(pIfIncorrectThenExitSubOrStop) = "S" Then
Msgbox "Incorrect data in cell >> " & ColumnNameOfColumnIndex(pCellPositionColumn) & pCellPositionRow + 1, ,"Error"
Stop
End If
End Select
ElseIf UCase(pMandatoryOrNonMandatory) = "NONMANDATORY" Or UCase(Left(pMandatoryOrNonMandatory,1)) = "N" Then
Select Case CheckCellContentType(pCellPositionColumn, pCellPositionRow) '[0=EMPTY/BLANK][1=VALUE/DATE][2=TEXT][3=FORMULA]
Case 0
pArrayElement = oSheet.getCellByPosition(pCellPositionColumn, pCellPositionRow).String 'Get blank as NULL
Case 1
' Set cell format to VALUE for sure.
ApplyNumberFormatToRange(pCellPositionColumn, pCellPositionRow, pCellPositionColumn, pCellPositionRow, "v")
pArrayElement = oSheet.getCellByPosition(pCellPositionColumn, pCellPositionRow).String
Case 2, 3
If UCase(pIfIncorrectThenExitSubOrStop) = "E" Then
JumpToNextRow = "Y"
GoTo ExitThisSub
End If
If UCase(pIfIncorrectThenExitSubOrStop) = "S" Then
Msgbox "Incorrect data in cell >> " & ColumnNameOfColumnIndex(pCellPositionColumn) & pCellPositionRow + 1, ,"Error"
Stop
End If
End Select
End If
ExitThisSub:
End Sub
Function CheckCellContentType%(pColumn&, pRow&)
Dim oDoc, oSheet, oCell As Object
oDoc = ThisComponent
oSheet = oDoc.getcurrentcontroller.activesheet
oCell = oSheet.getCellByPosition(pColumn, pRow)
Select Case oCell.Type
Case com.sun.star.table.CellContentType.EMPTY '0
CheckCellContentType = oCell.Type
Case com.sun.star.table.CellContentType.VALUE '1
CheckCellContentType = oCell.Type
Case com.sun.star.table.CellContentType.TEXT '2
CheckCellContentType = oCell.Type
Case com.sun.star.table.CellContentType.FORMULA '3
CheckCellContentType = oCell.Type
End Select
End Function
Function ApplyNumberFormatToRange(pStartColumn&, pStartRow&, pEndColumn&, pEndRow&, pDateNumberText$) As Variant
Dim oFormats As Object : oFormats = ThisComponent.NumberFormats
Dim oLocale As New com.sun.star.lang.Locale
Dim oRange As Object : oRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(pStartColumn, pStartRow, pEndColumn, pEndRow)
If UCase(pDateNumberText) = "D" Then : oRange.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.DATE, oLocale) : End If
If UCase(pDateNumberText) = "N" Then : oRange.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.NUMBER, oLocale) : End If
If UCase(pDateNumberText) = "T" Then : oRange.NumberFormat = oFormats.getStandardFormat(com.sun.star.util.NumberFormat.TEXT, oLocale) : End If
End Function