Newbie
Newbie

Reputation: 873

Libreoffice calc: loop throught cells macro

I've been searching a lot but could find little to no info about LibreOffice Basic

I'm a bit used to programming macros in excel but this time a need to do a loop until i reach the first empty column and it needs to be in libreoffice.

In excel i would do something like this:

Dim i As integer

i = 0
Range("A1").Select
While cell.Offset(0, i).Value <> Null
    i = i + 1
Wend
MsgBox ("First empty column is " & Chr(i + 64))

But in libreoffice i have no idea.

Can anyone help me.

Thanks, Bruno

Upvotes: 4

Views: 24824

Answers (2)

GuidoRafael
GuidoRafael

Reputation: 1

rem I had a similar problem to solve.
rem Update for libreoffice 7.
rem Replaced "sheet" with "ThisComponent.Sheets(0)". 
rem Thanks.


sub main 
dim cell   as object
dim i      as integer

i = 0
rem "sheet" alone does not run
cell = ThisComponent.Sheets(0).getCellByPosition(i,0) 

while Cell.Type <> com.sun.star.table.CellContentType.EMPTY
    i    = i+1
    cell = ThisComponent.Sheets(0).getCellByPosition(i,0)   
wend
MsgBox( i )
end sub 

Upvotes: 0

Newbie
Newbie

Reputation: 873

I managed to find the answer this way:

dim cell as object
dim i as integer

i = 0
cell = Sheet.getCellByPosition(i,0)

while Cell.Type <> com.sun.star.table.CellContentType.EMPTY
    i = i+1
    cell = Sheet.getCellByPosition(i,0)
wend

When the loop ends I get the variable i which corresponds to the column number. I can then convert it to the letter the same way as in excel (chr functions)

Upvotes: 17

Related Questions