libreoffice calc Macro to shift focused cell to the right by one cell

141 Views Asked by At

I have a cell in focus. I want to shift the focus to one cell immediately in the right using a macro. I've tried this but to no avail...

Sub move_cursor

sheet = ThisComponent.CurrentController.ActiveSheet
    cell = sheet.getCellRangeByName("A1")

    cursor = sheet.createCursorByRange(cell)
    MsgBox cursor.ImplementationName

'    Move to next cell
cursor.gotoNext()

' Trying to shift using offset to next cell on right.
cursor.gotoOffset(1, 0)

End Sub

Can anyone help?

3

There are 3 best solutions below

1
Micklos On BEST ANSWER

I found a LibreOffice BASIC macro that works (I'm on LO 7.6) from Jim K. Get Row and Column number from "ThisComponent.CurrentSelection" in libreoffice calc basic

But it's great that this page gives a number of diverse methods as offered by JohnSUN

5
JohnSUN On

Is moving the cursor to the right one cell the same as pressing the right arrow? Then the easiest way to create the desired script is to enable macro recording, press the right arrow, stop macro recording, give the macro a meaningful name. The result will be code similar to this

sub moveCursorRight
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "By"
args1(0).Value = 1
args1(1).Name = "Sel"
args1(1).Value = false

dispatcher.executeDispatch(document, ".uno:GoRight", "", 0, args1())
end sub

This will really get the job done.

Using the code snippets from my first comment, the macro code could be like this:

Sub jumpOneCellToRight
Dim nRow As Long, nColumn As Long, oSheet As Variant, oController As Variant 
    nRow = ActiveCell(2)
    nColumn = ActiveCell(1)
    oController = ThisComponent.getCurrentController()
    oSheet = oController.getActiveSheet()
    oController.Select(oSheet.getCellByPosition(nColumn+1,nRow))
    oController.Select(ThisComponent.createInstance("com.sun.star.sheet.SheetCellRanges"))
End Sub

Using VBA compatibility this can be written more shorter

Option VbaSupport 1

Sub goRight()
     Selection.Offset(0,1).Select
End Sub
0
Micklos On

Based on JohnSUN's (TQ) answer, I changed things a bit and now it works for me...

Sub BetterCloseDataEntry_jumpOneCellToRight

Dim nRow As Long, nColumn As Long, oSheet As Variant, oController As Variant 

    nColumn = ThisComponent.getCurrentController().getSelection().RangeAddress.StartColumn
    nRow = ThisComponent.getCurrentController().getSelection().RangeAddress.StartRow

    oController = ThisComponent.getCurrentController()
    oSheet = oController.getActiveSheet()
    oController.Select(oSheet.getCellByPosition(nColumn+1,nRow))
    oController.Select(ThisComponent.createInstance("com.sun.star.sheet.SheetCellRanges"))
End Sub

Avoiding any possible error from the method based around the Jim K solution (link to it is on this thread) featuring the line

If oSels.supportsService("com.sun.star.sheet.SheetCell")