I would like to duplicate the numbers from from excel sheet 1 to excel sheet 2.Kindly help me on this.Sometime its unable to duplicate..
Sub Duplicate
Dim oDoc As Object, oSheet As Object, oCell As Object, oCell2 As Object, oCell3 As Object, oString As String
Dim oCells As Object
Dim oCursors As Object
Dim aAddresss As Variant
REM Define what sheet to used
oDoc =ThisComponent
oSheet =oDoc.CurrentCo ntroller.Active Sheet
oSheet2=oDoc.Sh eets.getByIndex (1) '2nd Sheet
REM Get the value of the LastUsedRow & LastUsedColumn
oCells = oSheet.GetCellb yPosition(0, 0)
oCursors = oSheet.createCu rsorByRange(oCe lls)
oCursors.GotoEn dOfUsedArea(Tru e)
aAddress = oCursors.RangeA ddress
LastUsedRow = aAddress.EndRow
LastUsedColumn = aAddress.EndCol umn
'Row2Print=row printed row
'l=last used row in Sheet2
For i=0 to LastUsedRow
'oCell2=ThisCom ponent.CurrentS election.getCel lAddress 'Currently Selected Cell
oSelect=ThisCom ponent.CurrentS election.getRan geAddress
oString = oSheet.GetCellb yPosition(oSele ct.StartColumn, i).getString() 'IMPORTANT (Need Revision)
'getCellByPosit ion(Column,Row)
oRight = Val(Right(oStri ng,Len(oString)-InStr(1, oString, "-")))
oLeft = Val(Left(oStrin g,Len(oString)-InStr(1, oString, "-")))
Row2Print = oRight - oLeft
oRangeOrg = oSheet.getCellR angeByName("A"& (i+1)&":O"&(i+1 )).RangeAddress ' copy range
REM Begin Pasting the Value
For j=0 to Row2Print
k=k+1
oRangeCpy = oSheet2.getCell RangeByName("B" &k).RangeAddres s ' insert range
oCellCpy = oSheet2.getCell ByPosition(oRan geCpy.StartColu mn,oRangeCpy.St artRow).CellAdd ress ' insert position
oSheet.CopyRang e(oCellCpy, oRangeOrg) ' copy
Next
'oSheet2=oDoc.S heets.getByInde x(1) '2nd Sheet
'oCells = oSheet2.GetCell byPosition(0, 0)
'oCursors = oSheet2.createC ursorByRange(oC ells)
'oCursors.GotoE ndOfUsedArea(Tr ue)
'aAddress = oCursors.RangeA ddress
'LastUsedRow = aAddress.EndRow
'LastUsedColumn = aAddress.EndCol umn
For l=0 to Row2Print
oCell4=oSheet2. getCellByPositi on(0,m) 'A1
oCell4.setStrin g(oLeft)
oLeft=oLeft+1
m=m+1
Next
Next i
'oCell.NumberFo rmat=2 '23658.00
'oCell.SetValue (12345)
'oCell.SetStrin g("oops")
'oCell.setFormu la("=FUNCTION() ")
'oCell.IsCellBa ckgroundTranspa rent = TRUE
'oCell.CellBack Color = RGB(255,141,56)
End Sub
Function GetLastUsedRow( oSheets as Object) as Integer
Dim oCells As Object
Dim oCursors As Object
Dim aAddresss As Variant
oCells = oSheets.GetCell byPosition(0, 0)
oCursors = oSheets.createC ursorByRange(oC ells)
oCursors.GotoEn dOfUsedArea(Tru e)
aAddresss = oCursors.RangeA ddress
GetLastUsedRow = aAddresss.EndRo w
End Function
Function GetLastUsedColu mn(oSheet as Object) as Integer
Dim oCell As Object
Dim oCursor As Object
Dim aAddress As Variant
oCell = oSheet.GetCellb yPosition( 0, 0 )
oCursor = oSheet.createCu rsorByRange(oCe ll)
oCursor.GotoEnd OfUsedArea(True )
aAddress = oCursor.RangeAd dress
GetLastUsedColu mn = aAddress.EndCol umn
End Function
Sub SelRow()
Dim oSheet
Dim oRow
oSheet = ThisComponent.g etSheets().getB yIndex(0)
oRow = oSheet.getRows( ).getByIndex(2)
ThisComponent.g etCurrentContro ller().select(o Row)
End Sub
Sub CopySpreadsheet Range
oSheet1 = ThisComponent.S heets.getByInde x(0) ' sheet no 1, original
oSheet2 = ThisComponent.S heets.getByInde x(1) ' sheet no 2
oRangeOrg = oSheet1.getCell RangeByName("A1 :C10").RangeAdd ress ' copy range
oRangeCpy = oSheet2.getCell RangeByName("A1 :C10").RangeAdd ress ' insert range
oCellCpy = oSheet2.getCell ByPosition(oRan geCpy.StartColu mn,_
oRangeCpy.Start Row).CellAddres s ' insert position
oSheet1.CopyRan ge(oCellCpy, oRangeOrg) ' copy
End Sub
'----------------------------------------------------------------------------------------
Function IsSpreadsheetDo c(oDoc) As Boolean
Dim s$ : s$ = "com.sun.star.s heet.Spreadshee tDocument"
IsSpreadsheetDo c = oDoc.SupportsSe rvice(s$)
End Function
Sub checking( )
MsgBox IsSpreadsheetDo c(thisComponent )
End Sub
Sub ExampleGetValue
Dim oDoc As Object, oSheet As Object, oCell As Object
oDoc=ThisCompon ent
oSheet=oDoc.She ets.getByName(" Sheet1")
oCell=oSheet.ge tCellByposition (0,0) 'A1
Rem a cell's contents can have one of the three following types:
Print oCell.getValue( )
'Print oCell.getString ()
'Print oCell.getFormul a()
End Sub
Sub SelectedCells
oSelect=ThisCom ponent.CurrentS election.getRan geAddress
oSelectColumn=T hisComponent.Cu rrentSelection. Columns
oSelectRow=This Component.Curre ntSelection.Row s
CountColumn=oSe lectColumn.getC ount
CountRow=oSelec tRow.getCount
oSelectSC=oSele ctColumn.getByI ndex(0).getName
oSelectEC=oSele ctColumn.getByI ndex(CountColum n-1).getName
oSelectSR=oSele ct.StartRow+1
oSelectER=oSele ct.EndRow+1
NoCell=(CountCo lumn*CountRow)
If CountColumn=1 AND CountRow=1 Then
MsgBox("Cell " + oSelectSC + oSelectSR + chr(13) + "Cell No = " + NoCell,, "SelectedCells" )
Else
MsgBox("Range(" + oSelectSC + oSelectSR + ":" + oSelectEC + oSelectER + ")" + chr(13) + "Cell No = " + NoCell,, "SelectedCells" )
End If
End Sub
Sub Analize
sSum="=SUM("+Ge tAddress+")"
sAverage="=AVER AGE("+GetAddres s+")"
sMin="=MIN("+Ge tAddress+")"
sMax="=MAX("+Ge tAddress+")"
CellPos(7,6).se tString(GetAddr ess)
CellPos(7,8).se tFormula(sSum)
CellPos(7,8).Nu mberFormat=2
CellPos(7,10).s etFormula(sAver age)
CellPos(7,10).N umberFormat=2
CellPos(7,12).s etFormula(sMin)
CellPos(7,12).N umberFormat=2
CellPos(7,14).s etFormula(sMax)
CellPos(7,14).N umberFormat=2
End sub
Function GetAddress 'selected cell(s)
oSelect=ThisCom ponent.CurrentS election.getRan geAddress
oSelectColumn=T hisComponent.Cu rrentSelection. Columns
oSelectRow=This Component.Curre ntSelection.Row s
CountColumn=oSe lectColumn.getC ount
CountRow=oSelec tRow.getCount
oSelectSC=oSele ctColumn.getByI ndex(0).getName
oSelectEC=oSele ctColumn.getByI ndex(CountColum n-1).getName
oSelectSR=oSele ct.StartRow+1
oSelectER=oSele ct.EndRow+1
NoCell=(CountCo lumn*CountRow)
If CountColumn=1 AND CountRow=1 then
GetAddress=oSel ectSC+oSelectSR
Else
GetAddress=oSel ectSC+oSelectSR +":"+oSelectEC+ oSelectER
End If
End Function
Function CellPos(lColumn As Long,lRow As Long)
CellPos= ActiveSheet.get CellByPosition (lColumn,lRow)
End Function
Function ActiveSheet
ActiveSheet=Sta rDesktop.Curren tComponent.Curr entController.A ctiveSheet
End Function
Sub DeleteDbRange(s RangeName As String)
oRange=ThisComp onent.DatabaseR anges
oRange.removeBy Name (sRangeName)
End Sub
Sub Duplicate
Dim oDoc As Object, oSheet As Object, oCell As Object, oCell2 As Object, oCell3 As Object, oString As String
Dim oCells As Object
Dim oCursors As Object
Dim aAddresss As Variant
REM Define what sheet to used
oDoc =ThisComponent
oSheet =oDoc.CurrentCo ntroller.Active Sheet
oSheet2=oDoc.Sh eets.getByIndex (1) '2nd Sheet
REM Get the value of the LastUsedRow & LastUsedColumn
oCells = oSheet.GetCellb yPosition(0, 0)
oCursors = oSheet.createCu rsorByRange(oCe lls)
oCursors.GotoEn dOfUsedArea(Tru e)
aAddress = oCursors.RangeA ddress
LastUsedRow = aAddress.EndRow
LastUsedColumn = aAddress.EndCol umn
'Row2Print=row printed row
'l=last used row in Sheet2
For i=0 to LastUsedRow
'oCell2=ThisCom ponent.CurrentS election.getCel lAddress 'Currently Selected Cell
oSelect=ThisCom ponent.CurrentS election.getRan geAddress
oString = oSheet.GetCellb yPosition(oSele ct.StartColumn, i).getString() 'IMPORTANT (Need Revision)
'getCellByPosit ion(Column,Row)
oRight = Val(Right(oStri ng,Len(oString)-InStr(1, oString, "-")))
oLeft = Val(Left(oStrin g,Len(oString)-InStr(1, oString, "-")))
Row2Print = oRight - oLeft
oRangeOrg = oSheet.getCellR angeByName("A"& (i+1)&":O"&(i+1 )).RangeAddress ' copy range
REM Begin Pasting the Value
For j=0 to Row2Print
k=k+1
oRangeCpy = oSheet2.getCell RangeByName("B" &k).RangeAddres s ' insert range
oCellCpy = oSheet2.getCell ByPosition(oRan geCpy.StartColu mn,oRangeCpy.St artRow).CellAdd ress ' insert position
oSheet.CopyRang e(oCellCpy, oRangeOrg) ' copy
Next
'oSheet2=oDoc.S heets.getByInde x(1) '2nd Sheet
'oCells = oSheet2.GetCell byPosition(0, 0)
'oCursors = oSheet2.createC ursorByRange(oC ells)
'oCursors.GotoE ndOfUsedArea(Tr ue)
'aAddress = oCursors.RangeA ddress
'LastUsedRow = aAddress.EndRow
'LastUsedColumn = aAddress.EndCol umn
For l=0 to Row2Print
oCell4=oSheet2. getCellByPositi on(0,m) 'A1
oCell4.setStrin g(oLeft)
oLeft=oLeft+1
m=m+1
Next
Next i
'oCell.NumberFo rmat=2 '23658.00
'oCell.SetValue (12345)
'oCell.SetStrin g("oops")
'oCell.setFormu la("=FUNCTION() ")
'oCell.IsCellBa ckgroundTranspa rent = TRUE
'oCell.CellBack Color = RGB(255,141,56)
End Sub
Function GetLastUsedRow( oSheets as Object) as Integer
Dim oCells As Object
Dim oCursors As Object
Dim aAddresss As Variant
oCells = oSheets.GetCell byPosition(0, 0)
oCursors = oSheets.createC ursorByRange(oC ells)
oCursors.GotoEn dOfUsedArea(Tru e)
aAddresss = oCursors.RangeA ddress
GetLastUsedRow = aAddresss.EndRo w
End Function
Function GetLastUsedColu mn(oSheet as Object) as Integer
Dim oCell As Object
Dim oCursor As Object
Dim aAddress As Variant
oCell = oSheet.GetCellb yPosition( 0, 0 )
oCursor = oSheet.createCu rsorByRange(oCe ll)
oCursor.GotoEnd OfUsedArea(True )
aAddress = oCursor.RangeAd dress
GetLastUsedColu mn = aAddress.EndCol umn
End Function
Sub SelRow()
Dim oSheet
Dim oRow
oSheet = ThisComponent.g etSheets().getB yIndex(0)
oRow = oSheet.getRows( ).getByIndex(2)
ThisComponent.g etCurrentContro ller().select(o Row)
End Sub
Sub CopySpreadsheet Range
oSheet1 = ThisComponent.S heets.getByInde x(0) ' sheet no 1, original
oSheet2 = ThisComponent.S heets.getByInde x(1) ' sheet no 2
oRangeOrg = oSheet1.getCell RangeByName("A1 :C10").RangeAdd ress ' copy range
oRangeCpy = oSheet2.getCell RangeByName("A1 :C10").RangeAdd ress ' insert range
oCellCpy = oSheet2.getCell ByPosition(oRan geCpy.StartColu mn,_
oRangeCpy.Start Row).CellAddres s ' insert position
oSheet1.CopyRan ge(oCellCpy, oRangeOrg) ' copy
End Sub
'----------------------------------------------------------------------------------------
Function IsSpreadsheetDo c(oDoc) As Boolean
Dim s$ : s$ = "com.sun.star.s heet.Spreadshee tDocument"
IsSpreadsheetDo c = oDoc.SupportsSe rvice(s$)
End Function
Sub checking( )
MsgBox IsSpreadsheetDo c(thisComponent )
End Sub
Sub ExampleGetValue
Dim oDoc As Object, oSheet As Object, oCell As Object
oDoc=ThisCompon ent
oSheet=oDoc.She ets.getByName(" Sheet1")
oCell=oSheet.ge tCellByposition (0,0) 'A1
Rem a cell's contents can have one of the three following types:
Print oCell.getValue( )
'Print oCell.getString ()
'Print oCell.getFormul a()
End Sub
Sub SelectedCells
oSelect=ThisCom ponent.CurrentS election.getRan geAddress
oSelectColumn=T hisComponent.Cu rrentSelection. Columns
oSelectRow=This Component.Curre ntSelection.Row s
CountColumn=oSe lectColumn.getC ount
CountRow=oSelec tRow.getCount
oSelectSC=oSele ctColumn.getByI ndex(0).getName
oSelectEC=oSele ctColumn.getByI ndex(CountColum n-1).getName
oSelectSR=oSele ct.StartRow+1
oSelectER=oSele ct.EndRow+1
NoCell=(CountCo lumn*CountRow)
If CountColumn=1 AND CountRow=1 Then
MsgBox("Cell " + oSelectSC + oSelectSR + chr(13) + "Cell No = " + NoCell,, "SelectedCells" )
Else
MsgBox("Range(" + oSelectSC + oSelectSR + ":" + oSelectEC + oSelectER + ")" + chr(13) + "Cell No = " + NoCell,, "SelectedCells" )
End If
End Sub
Sub Analize
sSum="=SUM("+Ge tAddress+")"
sAverage="=AVER AGE("+GetAddres s+")"
sMin="=MIN("+Ge tAddress+")"
sMax="=MAX("+Ge tAddress+")"
CellPos(7,6).se tString(GetAddr ess)
CellPos(7,8).se tFormula(sSum)
CellPos(7,8).Nu mberFormat=2
CellPos(7,10).s etFormula(sAver age)
CellPos(7,10).N umberFormat=2
CellPos(7,12).s etFormula(sMin)
CellPos(7,12).N umberFormat=2
CellPos(7,14).s etFormula(sMax)
CellPos(7,14).N umberFormat=2
End sub
Function GetAddress 'selected cell(s)
oSelect=ThisCom ponent.CurrentS election.getRan geAddress
oSelectColumn=T hisComponent.Cu rrentSelection. Columns
oSelectRow=This Component.Curre ntSelection.Row s
CountColumn=oSe lectColumn.getC ount
CountRow=oSelec tRow.getCount
oSelectSC=oSele ctColumn.getByI ndex(0).getName
oSelectEC=oSele ctColumn.getByI ndex(CountColum n-1).getName
oSelectSR=oSele ct.StartRow+1
oSelectER=oSele ct.EndRow+1
NoCell=(CountCo lumn*CountRow)
If CountColumn=1 AND CountRow=1 then
GetAddress=oSel ectSC+oSelectSR
Else
GetAddress=oSel ectSC+oSelectSR +":"+oSelectEC+ oSelectER
End If
End Function
Function CellPos(lColumn As Long,lRow As Long)
CellPos= ActiveSheet.get CellByPosition (lColumn,lRow)
End Function
Function ActiveSheet
ActiveSheet=Sta rDesktop.Curren tComponent.Curr entController.A ctiveSheet
End Function
Sub DeleteDbRange(s RangeName As String)
oRange=ThisComp onent.DatabaseR anges
oRange.removeBy Name (sRangeName)
End Sub
Comment