user1855639
user1855639

Reputation: 1

How to add cell elements of 2 columns in libreOffice Calc Macro?

I have 2 columns say column G and column H. Each one has different numeric values till 1449 rows. Now, I want to add corresponding cell elements at every row to get a new column. Is there any function to do it because simple + is not working in .DataArray() function.

Sub updateSTOCK

  Dim my_range,my_range2

  my_range=ThisComponent.Sheets(3).getCellRangebyName("H5:H1449")
  my_range2=ThisComponent.Sheets(3).getCellRangebyName("G5:G1449")

  my_range.Data=my_range.Data+my_range2.Data    ***----> This line gives error***

End Sub

Upvotes: 0

Views: 568

Answers (1)

JohnSUN
JohnSUN

Reputation: 2539

In your code, you must perform addition for each of the lines; group operations on range values are not supported

Sub updateSTOCK()
Dim oSheets As Variant
Dim oSheet3 As Variant
Dim oCellRangeByName As Variant
Dim oDataArray As Variant
Dim i As Long
    oSheets = ThisComponent.getSheets()
    oSheet3 = oSheets.getByIndex(3)
    oCellRangeByName = oSheet3.getCellRangeByName("H5:G1449")
    oDataArray = oCellRangeByName.getDataArray()
    For i = LBound(oDataArray) To UBound(oDataArray)
        oDataArray(i)(1) = oDataArray(i)(0) + oDataArray(i)(1)
    Next i
    oCellRangeByName.setDataArray(oDataArray)
End Sub

Upvotes: 0

Related Questions