I try to insert a formula in a cell like in the subject header so in full
Code: Select all
osheet.GetCellbyPosition(24,2).formula= "=RC[4]"
If I correct this by hand to UPPER CASE it works.
Please help!!!!!
Dick
Code: Select all
osheet.GetCellbyPosition(24,2).formula= "=RC[4]"
Code: Select all
Const cSheetPrefix% = 1
Const cAbsoluteSheet% = 2
Const cAbsoluteStartColumn% = 4
Const cAbsoluteStartRow% = 8
Const cAbsoluteEndColumn% = 16
Const cAbsoluteEndRow% = 32
'returns: any variant of string-address (eg. A1:B5,sheet.A1:B5,$sheet.A1:B5,sheet.$A1:$B5,...,$sheet.$A$1:$B$5)
'takes a spreasheet-doc, a range and optional flags(default returns relative A1:B5 without sheet name)
Function getRangeName(oRg,Optional iFlag%)as string
on error goto exitErr
Dim oAddr, oSh
Dim s$,sSh$,sC1$,sC2$,sR1$,sR2$,bR1 as boolean,bC2 as boolean,bR2 as boolean
oAddr = oRg.getRangeAddress()
oSh = oRg.getSpreadSheet()
sSh = oSh.getName()
sC1 = oSh.Columns.getByIndex(oAddr.StartColumn).getName()
sC2 = oSh.Columns.getByIndex(oAddr.EndColumn).getName()
sR1 = cStr(oAddr.StartRow +1)
sR2 = cStr(oAddr.EndRow +1)
if not isMissing(iFlag) then
if iFlag AND cSheetPrefix then
if (iFlag AND cAbsoluteSheet)=cAbsoluteSheet then s = "$"
s = s & sSh &"."
end if
if iFlag AND cAbsoluteStartColumn then s = s &"$"
bR1 = iFlag AND cAbsoluteStartRow
bC2 = iFlag AND cAbsoluteEndColumn
bR2 = iFlag AND cAbsoluteEndRow
end if
s = s & sC1
If bR1 then s = s & "$"
s = s & sR1
s = s &":"
if bC2 then s = s & "$"
s = s & sC2
if bR2 then s = s &"$"
s = s & sR2
getRangeName = s
exitErr:
end function
Function getCellName(oCell,Optional iFlag%)as string
on error goto exitErr
Dim oAddr, oSh
Dim s$,sSh$,sC1$,sR1$,bR1 as boolean
oAddr = oCell.getRangeAddress()
oSh = oCell.getSpreadSheet()
sSh = oSh.getName()
sC1 = oSh.Columns.getByIndex(oAddr.StartColumn).getName()
sR1 = cStr(oAddr.StartRow +1)
if not isMissing(iFlag) then
if iFlag AND cSheetPrefix then
if (iFlag AND cAbsoluteSheet)=cAbsoluteSheet then s = "$"
s = s & sSh &"."
end if
if iFlag AND cAbsoluteStartColumn then s = s &"$"
bR1 = iFlag AND cAbsoluteStartRow
end if
s = s & sC1
If bR1 then s = s & "$"
s = s & sR1
getCellName = s
exitErr:
end function
Edit: The striken text is a duplicate of BASIC runtime error; Argument is not optional Volunteers: Do not respond here about this runtime error, only there djbierman: Please don't multi-post. It is considered rude. -- MrProgrammer, forum moderator |
Code: Select all
function test(n as integer) as string
dim s(26) as string
s(0)=" "
s(1)="A"
s(2)="B"
' etc.
s(25)="C"
s(26)="Z"
test = s(n)
end function
@djbierman, your problem is that you are using a different notation (not supported directly, Calc A1 syntax is supported only)djbierman wrote:I try to insert a formula in a cell...BTW is any calc-function usable in Basic?Code: Select all
oSheet.getCellByPosition(24, 2).Formula = "=RC[4]"
Code: Select all
Sub Main
Dim oSheet As Object, oCell As Object
oSheet = ThisComponent.CurrentController.ActiveSheet
oCell = oSheet.GetCellByPosition(24, 2)
Call SetFormulaR1C1("=RC[4]", oCell)
End Sub
Sub SetFormulaR1C1(sFormulaR1C1$, oCell As Object)
Dim oParser As Object
oParser = ThisComponent.CreateInstance("com.sun.star.sheet.FormulaParser")
oParser.FormulaConvention = com.sun.star.sheet.AddressConvention.XL_R1C1
oCell.Tokens = oParser.parseFormula(sFormulaR1C1, oCell.CellAddress)
End Sub
Why? Perhaps this will help (see: UserInterfaceRepresentation).djbierman wrote:I wrote a simple function to convert a column number to reference in Calc A1 notation...
Code: Select all
Sub Main()
Dim oSheet As Object, oCell As Object
Dim oConv As Object 'cell address conversion service (ScAddressConversionObj - deprecated)
Dim s$
Rem oCell = ThisComponent.CurrentSelection
oSheet = ThisComponent.CurrentController.ActiveSheet
oCell = oSheet.GetCellByPosition(24, 2)
' An example of using the CellAddressConversion object.
oConv = ThisComponent.createInstance("com.sun.star.table.CellAddressConversion")
oConv.Address = oCell.CellAddress
s = oConv.UserInterfaceRepresentation
' ?BUG: If it's not the 1st sheet, then the sheet name is not skipped.
s = Mid(s, InStr(s, ".") + 1) 'omits the name just in case
MsgBox "Cell Name: " & s, Title:="Active Cell Name"
End Sub
The INDIRECT function supports R1C1.djbierman wrote:osheet.GetCellbyPosition(24,2).formula= "=RC[4]"
Sub T107294
Dim oSheet As Object
oSheet = ThisComponent.Sheets(0)
oSheet.getCellByPosition(24,2).Formula= "=INDIRECT(""RC[4]"";0)"
End Sub
However the target cell at position (24,2) is known to be Y3, so why not use the simple formula =AC3?You will continue to struggle until you become familar with this material: http://www.pitonyak.org/oo.php. Expect to spend a week or more learning how to use the complex Application Programming Interface. It will take even longer if you aren't already a programmer. It's important to understand expectations before you begin projects. The API's design is quite clever, but it takes everyone, even experienced programmers, a long time to appreciate its genius and learn how to use it effectively. Do not be discouraged if the learning curve is steep and long. Volunteers like Villeroy, JeJe, and Zizi64 might make macro programming look easy. It's not.djbierman wrote:I am a beginner so please excuse me if this is a trivial problem.