[Solved] Delete all blank cells and shift up

Discuss the spreadsheet application
Post Reply
cul
Posts: 62
Joined: Mon Jan 29, 2018 1:15 pm

[Solved] Delete all blank cells and shift up

Post by cul »

Is this possible?
Last edited by cul on Thu May 21, 2020 7:48 am, edited 1 time in total.
LibreOffice on Windows 7 (sometimes Mac)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Delete all blank cells and shift up

Post by Villeroy »

The following macro selects all blanks within the current selection of cells:

Code: Select all

Sub SelectBlanks()
  view = ThisComponent.getCurrentController()
  x = view.Selection.queryEmptyCells()
  view.select(x)
End Sub
With this selection you can delete rows.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Lupp
Volunteer
Posts: 3552
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Delete all blank cells and shift up

Post by Lupp »

The multiselection allows to clear its cells from contents, but not to remove the cells from the sheet (filling columns up from the bottom). [Removal of complete rows or columns is supported.]
What the questioner asked for would be done by:

Code: Select all

Sub removeEmptyCellRangesFromActiveSheet()      REM Very radical!
doc      = ThisComponent
cCtrl    = doc.CurrentController
sheet    = cCtrl.ActiveSheet
cur      = sheet.createCursor
cur.gotoStartOfUsedArea(False)
cur.gotoEndOfUsedArea(True)
curCols  = cur.Columns
For Each col In curCols
  eRgs     = col.queryEmptyCells
  u        = eRgs.Count-1                       REM Reverse order necessary!
  For j = u To 0 Step - 1                       REM Otherwise a new query needed before the next step!
    sheet.removeRange(eRgs.RangeAddresses(j), 1)REM com.sun.star.sheet.CellDeleteMode.UP = 1
  Next j
Next col
End Sub
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply