OpenOffice Calc macro: Find last column with data

Sub ReplaceInUsedRange
   oDocument = ThisComponent
   oSheets = oDocument.Sheets
   oSheet = oSheets.getByIndex(0)
   oCellCursor = oSheet.createCursor()

   oCellCursor.gotoStartOfUsedArea( False )
   nFirstRow = oCellCursor.getRangeAddress().StartRow
   nFirstCol = oCellCursor.getRangeAddress().StartColumn

   oCellCursor.gotoEndOfUsedArea( False )
   nLastRow = oCellCursor.getRangeAddress().EndRow
   nLastCol = oCellCursor.getRangeAddress().EndColumn

   oRange = oSheet.getCellRangeByPosition(nFirstCol,nFirstRow,nLastCol,nLastRow)
   vDescriptor = oRange.createReplaceDescriptor()
   With vDescriptor
      .SearchString = "SearchString"
      .ReplaceString = "ReplaceString"
      .SearchWords = False
      .SearchCaseSensitive = False
   End With
   nReplaced = oRange.replaceAll( vDescriptor )
   MsgBox "Replaced " & nReplaced & " times."
End Sub



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>