Search This Blog

23 August 2011

Open Office Star Basic macro to select sheet and write a summary to cells in different sheet


open office




REM ***** BASIC *****

Sub summary_odf()
' Author : Prasad Sirinayake
' Date : 19/08/2011

Dim oSheet1 as Object, oCell1 as Object, oDoc as Object
Dim oSheet as Object, oCel as Object

oDoc = ThisComponent

oSheet1 = oDoc.getSheets().getByName( "Sheet1" )
oCell1 = oSheet1.getCellRangeByName("a4")
ThisComponent.CurrentController.select(oCell1)

oAddress1 = oCell1.getCellAddress()
oCol1 = oAddress1.column
oRow1 = oAddress1.row

oSheet = oDoc.getSheets().getByName( "salaries" )
oCell = oSheet.getCellRangeByName("A6")
ThisComponent.CurrentController.select(oCell)

oAddress = oCell.getCellAddress()
oCol = oAddress.column
oRow = oAddress.row

Do While oCell.String <> ""

branch = oCell.String
if trim(oSheet.getCellByPosition(oCol+1,oRow+2).String) = "GENERAL SALES" then
gensale = trim(oSheet.getCellByPosition(oCol+1,oRow+5).Value)
endif
if trim(oSheet.getCellByPosition(oCol+2,oRow+2).String) = "LIFE SALES" then
lifesale = trim(oSheet.getCellByPosition(oCol+2,oRow+5).Value)
endif
if trim(oSheet.getCellByPosition(oCol+3,oRow+2).String) = "Others" then
others = trim(oSheet.getCellByPosition(oCol+3,oRow+5).Value)
endif
' Sheets("salaries").Select

ThisComponent.CurrentController.select("sheet1")

oCell1.String = branch
oCell1 = oSheet1.getCellByPosition(oCol1+1,oRow1)
oCell1.Value = gensale
oCell1 = oSheet1.getCellByPosition(oCol1+2,oRow1)
oCell1.Value = lifesale
oCell1 = oSheet1.getCellByPosition(oCol1+3,oRow1)
oCell1.Value = others
oRow1 = oRow1 + 1
oCell1 = oSheet1.getCellByPosition(oCol1,oRow1)

ThisComponent.CurrentController.select("salaries")

oRow = oRow + 9
oCell = oSheet.getCellByPosition(oCol,oRow)

gensale = 0
lifesale = 0
others = 0

loop

oCell1 = oSheet1.getCellRangeByName("a4")
ThisComponent.CurrentController.select(oCell1)

MsgBox "Macro Completed "
End Sub

1 comment:

  1. Hello Prasad,

    I have a question.

    how can i open a file and write data to a specific worksheet?
    I am already able to open a file and write data, but i want to write it to a specific worksheet.

    hopefully you can help me with this.

    Thanks,
    Rekesh

    ReplyDelete

How to Organize Virtual DJ Music Library using Shazam

  How to Organize Virtual DJ Music Library using Shazam Please Visit our YouTube channel and view our video on  following Video Link 1 Maste...