Clear cell contents Macro help

Discuss the spreadsheet application
Post Reply
Trevor_K
Posts: 2
Joined: Mon Feb 11, 2008 10:01 pm

Clear cell contents Macro help

Post by Trevor_K »

Hi, am converting a sheet to Calc from Excel and require to clear range of cells prior to filling with new data. (some cells must contain no data).
Have cut and pasted from various sites but each time I get:-
"Basic runtime Error" An Eception occurred $(ARG1).

EXAMPLE1 from Andrew Pitonyak

Code: Select all

Sub ClearDefinedRange

Dim Doc As Object
Dim Sheet As Object
Dim CellRange As Object
Dim Flags As Long

Doc = StarDesktop.CurrentComponent
Sheet = Doc.Sheets(0)
CellRange = Sheet.getCellRangeByName("B71:E83")

Flags = com.sun.star.sheet.CellFlags.STRING + _  <<< ERROR POINTS TO THIS LINE
      com.sun.star.sheet.CellFlags.HARDATTR

CellRange.clearContents(Flags)

End Sub
EXAMPLE 2 from Star Office site

Code: Select all

Sub ClearCellContents

Dim nFlags As Long
Dim oRange As New com.sun.star.table.CellRangeAddress

oRange = oSheet.getCellRangeByName( "B71:E83" )
 
nFlags = com.sun.star.sheet.CellFlags.STRING + _ <<< ERROR POINTS TO THIS LINE
        com.sun.star.sheet.CellFlags.STYLES
 
oRange.clearContents( nFlags ) 

End Sub
Am I missing an OOo lib or something similar. Any help appreciated.
User avatar
kingfisher
Volunteer
Posts: 2123
Joined: Tue Nov 20, 2007 10:53 am

Re: Clear cell contents Macro help

Post by kingfisher »

You should install the Xray tool and the SDK. The former is on the developers' page of http://www.ooomacros.org The latter is probably available from the mirrors.

First, the information about clearContents. That contains a link to the flags page.

I find it simpler to use numerical values. To delete strings only :

Code: Select all

CellRange.clearContents( 4 )
To clear simple values (i.e. not dates or times) :

Code: Select all

CellRange.clearContents( 1 )
To clear both strings and simple values :

Code: Select all

CellRange.clearContents( 5 )
Apache OpenOffice 4.1.9 on Linux
Trevor_K
Posts: 2
Joined: Mon Feb 11, 2008 10:01 pm

Re: Clear cell contents Macro help

Post by Trevor_K »

Your info much appreciated, seems I need to much more research/reading before diving into OOo macro's.

Thanks again.
martius
Posts: 56
Joined: Sat Feb 05, 2011 9:41 pm
Location: São Paulo, Brasil

Re: Clear cell contents Macro help

Post by martius »

If you want to clear eveything this is the best way:

Code: Select all

Sub clearEverything
Thiscomponent.CurrentSelection.ClearContents(1 OR 2 OR 4 OR 8 OR 16 OR 32 OR 64 OR 128 OR 256 OR 512)
End Sub
'constants group CellFlags
'com.sun.star.sheet.CellFlags.
'These constants select different types of cell contents.
'The values can be combined. They are used to insert, copy, or delete contents.

'com.sun.star.sheet.CellFlags.
'VALUE = 1 ->selects constant numeric values that are not formatted as dates or times.
'DATETIME = 2 ->selects constant numeric values that have a date or time number format.
'STRING = 4 ->selects constant strings.
'ANNOTATION = 8 ->selects cell annotations.
'FORMULA = 16 ->selects formulas.
'HARDATTR = 32 ->selects all explicit formatting, but not the formatting which is applied implicitly through style sheets.
'STYLES = 64 ->selects cell styles.
'OBJECTS = 128 ->selects drawing objects.
'EDITATTR = 256 ->selects formatting within parts of the cell contents.
'FORMATTED = 512 ->selects cells with formatting within the cells or cells with more than one paragraph within the cells.
LibreOffice 6.2.8.2 (x64), Windows 10 Home
paulschud
Posts: 1
Joined: Tue Jan 15, 2019 4:15 pm

Re: Clear cell contents Macro help

Post by paulschud »

or:

Code: Select all

Sub clearEverything
For i = 0 To 9
  Thiscomponent.CurrentSelection.ClearContents(2 ^ i)
Next
End Sub
LibreOffice 5.1.6.2 on Ubuntu 16.04 LTS
Post Reply