First, let me tell you what your macro does.
You clicked in a specific cell, which made that cell the current cell. In this
case you clicked in Cell E4, so these lines in your macro set the cell E4 to be
current.
args1(0).Name = "ToPoint"
args1(0).Value = "$E$4"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
Next, you enter “Input Mode”
dispatcher.executeDispatch(document, ".uno:SetInputMode", "", 0, Array())
And finally, you enter the string 132, which is NOT simply removing the first
character.
args3(0).Name = "StringName"
args3(0).Value = "132"
dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())
What you want to do is to check to see where the cursor is located or to check
what cells are selected rather than selecting a specific cell.
Sub Net_2
rem Make sure that at least one cell is selected.
Dim oSelections
Dim oCell
Dim s As String
oSelections = ThisComponent.getCurrentSelection()
' Could print an error, but just get out instead.
If IsNull(oSelections) Then Exit Sub
If oSelections.supportsService("com.sun.star.sheet.SheetCell") Then
'Print "One Cell selected = " & oSelections.getImplementationName()
'MsgBox "getString() = " & oSelections.getString()
ElseIf oSelections.supportsService("com.sun.star.sheet.SheetCellRange") Then
Rem So a single block of cells selected. I could process all of these, but
Rem I won't bother. I only want to work for a single cell.
Print "One Cell Range selected = " & oSelections.getImplementationName()
Exit Sub
ElseIf oSelections.supportsService("com.sun.star.sheet.SheetCellRanges") Then
Rem Multiple cell ranges selected, just ignore this.
Print "Multiple Cell Ranges selected = " &_
oSelections.getImplementationName()
Print "Count = " & oSelections.getCount()
Exit Sub
Else
Rem OK, I did not expect this!
Print "Something else selected = " & oSelections.getImplementationName()
Exit Sub
End If
Rem If we get here, then only one cell is selected.
Rem Lets be really stupid here rather than smart!
Rem No reason to do this, but makes it easier to read.
oCell = oSelections
Rem I am a bit concerned about what the cell contains.
Rem The cell may contain a formula, value, text, or be empty.
If oCell.getType() = com.sun.star.table.CellContentType.EMPTY Then
Rem Nothing to do, the cell is empty
ElseIf oCell.getType() = com.sun.star.table.CellContentType.FORMULA Then
Rem Don't think we should do anything if there is a formula in the cell
ElseIf oCell.getType() = com.sun.star.table.CellContentType.TEXT Then
s = oCell.getString()
If Len(s) < 2 Then
Rem Less than two charcters so just set the string to be empty
oCell.setString("")
Else
Rem Remove the leading character.
Rem To be really paranoid, I should get the text object
Rem and remove the first character so that I can
Rem retain any formating. This pretty much killls any formatting
Rem but you probably really do not want to see how to do that.
oCell.setString(Right(s, Len(s) - 1))
End If
ElseIf oCell.getType() = com.sun.star.table.CellContentType.VALUE Then
Rem Anything I do here is very dangerous, but I will do it anyway.
Rem I can use Str() to convert without localization
Rem I can use CStr() to convert using localization.
Rem An extra space is included in the string so remove it.
Rem This feels dangerous, but it probably kind of works!
Rem I am not checking for things such as dates. Everything about this
Rem feels dangerous.
s = Trim(Str(oCell.getValue()))
s = Right(s, Len(s) - 1)
Dim d As Double
d = s
oCell.setValue(d)
End If
End Sub
On Monday, July 28, 2025 20:07 EDT, Dave Close <[email protected]> wrote:
Near as I can tell, the way to do this is with a macro. I've tried
following several guides but it isn't working. What I want is really
simple: Select a shortcut (say F3) and have my spreadsheet respond as
though I had pressed four keys, F2 to start editing a cell, HOME to
position my cursor at the start of the cell, DEL to delete the first
character in the cell, ENTER to finish editing. But when I tried to
record this macro, nothing happened until I selected a cell. Then when
I tried to run the macro, the current cell was changed to contain what
the cell contained where recording was performed. What am I doing wrong?
I've looked at the macro code generated and don't see anything related
to what I want. I'd try to fix it but have no clue what to change. Here's
the macro which was recorded.
sub Neg
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(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$E$4"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:SetInputMode", "", 0, Array())
rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "StringName"
args3(0).Value = "132"
dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, args3())
end sub
Yeah, E4 was the cell where I tried recording and its value at that time
was 132. But I don't want to always do this only on that cell and I don't
want the result to always be 132.
--
Dave Close, Compata, Irvine CA +1 714 434 7359
[email protected] [email protected]
"Punctuality is the virtue of the bored." -- Evelyn Waugh
--
To unsubscribe e-mail to: [email protected]
Problems? https://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: https://wiki.documentfoundation.org/Netiquette
List archive: https://listarchives.libreoffice.org/global/users/
Privacy Policy: https://www.documentfoundation.org/privacy
--
To unsubscribe e-mail to: [email protected]
Problems? https://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: https://wiki.documentfoundation.org/Netiquette
List archive: https://listarchives.libreoffice.org/global/users/
Privacy Policy: https://www.documentfoundation.org/privacy