Hello!
I try to write a macro code. I have a problem. I want to get a value of a cell and I want to multiplication it with other cell and paste it.
I tried to multiplication it with many ways and I couldn’t it and I searched and couldn’t find it. Becauase of this I write here.
My code is here:
Sub CopyPaste
xA = CreateUnoStruct("com.sun.star.table.CellAddress")
yA = CreateUnoStruct("com.sun.star.table.CellAddress")
tA = CreateUnoStruct("com.sun.star.table.CellAddress")
zA = CreateUnoStruct("com.sun.star.table.CellAddress")
With xA
.Sheet = 0
.Column = 1
.Row = iCount6
End With
With yA
.Sheet = 0
.Column = 1
.Row = iCount6
End With
With tA 'For quantity
.Sheet = 0
.Column = 2
.Row = iCount6
End With
With zA
.Sheet = 0
.Column = 2
.Row = iCount6
End With
For iCount6 = 1 to 6 'loops through rows 1 to 6
xA.Row = iCount6
yA.Row = iCount6
tA.Row = iCount6
zA.Row = iCount6
SourceCell = ThisComponent.Sheets(xA.Sheet).GetCellByPosition(xA.Column, xA.Row)
SourceCell = ThisComponent.Sheets(tA.Sheet).GetCellByPosition(tA.Column, tA.Row)
PasteCell = (ThisComponent.Sheets(yA.Sheet).GetCellByPosition(yA.Column+2, yA.Row))*(ThisComponent.Sheets(zA.Sheet).GetCellByPosition(zA.Column+1, zA.Row))
PasteCell.DataArray = SourceCell.DataArray
Next iCount6
End Sub
In the other way I tried to get value of a cell. I added its code here.
Sub CopyPaste1
xA = CreateUnoStruct("com.sun.star.table.CellAddress")
yA = CreateUnoStruct("com.sun.star.table.CellAddress")
dim document as object
document = ThisComponent
sheet4 = document.Sheets(0)
For iCount6 = 1 to 6
With xA
.Sheet = 0
.Column = 1
.Row = iCount6
End With
With yA
.Sheet = 0
.Column = 1
.Row = iCount6
End With
MsgBox(sheet4.getCellByPosition(2, iCount6).Integer)
xA.Row = iCount6
yA.Row = iCount6
SourceCell = ThisComponent.Sheets(xA.Sheet).GetCellByPosition(xA.Column, xA.Row)
PasteCell = ThisComponent.Sheets(yA.Sheet).GetCellByPosition(yA.Column+2, yA.Row)*(sheet4.getCellByPosition(2, iCount6).Integer)
PasteCell.DataArray = SourceCell.DataArray
Next iCount6
End Sub
But here there is a problem about integer. If I write string, It works but not I want. I want to paste cell with quantity. So I can’t muliplication with String data.
I added example about them.
Copy-Paste Multiplication with a cell Example.ods
Edit for new example: 1.ods
Edit for @JohnSUN :
Sub copyMult
Dim oSheet As Variant, oRange As Variant
Dim oData As Variant ' Data of range, all cells of all rows in range '
Dim i As Long
oSheet = ThisComponent.getSheets().getByIndex(0)
Rem Get entire range - this is a bit simpler than describing each cell
oRange = oSheet.getCellRangeByName("B2:D7")
Rem Get values of all cells in ramge to array
oData = oRange.getDataArray()
Rem Repeat for each row in array
' For i = LBound(oData) To UBound(oData)
' For i = 2 To 7
Rem Take the integer part of values of the first and second cells and multiply it
Rem Function CInt converts any string or numeric expression to an integer
'Const sText3 = "Tahminleri Kopyalama Tamamlandı"
' MsgBox(CInt(oData(2)(2)))
oData(3)(2) = CInt(oData(1)(2)) * CInt(oData(2)(2))
' Next i
Rem Return the computed array to the range
oRange.setDataArray(oData)
End Sub
Thanks for your help.
Best Regard