Microsoft 365 Position curseur quand double clic dans ma cellule

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Bonjour à toutes et à tous,
J'espère que vous allez bien :)

Toujours dans ma recherche de gains de temps et d'éviter les erreurs,
Je bute sur un codage que je n'arrive pas à faire malgré mes tentatives et recherches.

Quand on double clic dans une cellule, le curseur se positionne automatiquement là où on clique dans la cellule et souvent au milieu du texte existant..
Je voudrais, quand je double clic dans ma cellule : que le curseur se positionne après le texte
ce qui éviterait d'écrire par erreur dans le texte existant.

Auriez-vous la solution ?

Je joins une petit fichier test
Avec mes remerciements,
Je vous souhaite une belle journée,
Amicalement,
lionel,
 

Pièces jointes

  • curseur_position.xlsm
    13.1 KB · Affichages: 49
Solution
bien vu job75
VB:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("s7:s20000")) Is Nothing Then
        Application.EnableEvents = False
        With Target
            .Value = Trim(Target.Value) & IIf(Right(Trim(Target.Value), 1) = "-", " ", " - ")
           If .Value = " - " Then .Value = ""
            Application.SendKeys ("{Down " & Len(.Value) & "}")
            Application.SendKeys "" 'visiblement a pour effet d'annuler la touche precedente donc pas d'association de touche
            'qui ammene la plupart du temps a transformer les touches 4,6,2,8 en fleche et annule les autre du pavé
        End With
    End If
    Application.EnableEvents = True...

job75

XLDnaute Barbatruc
Eh bien il en faudra un de plus car ma macro ne va pas si l'on entre 2 tirets séparés par un espace.

Il faut introduire l'espace dans la chaîne "=-+ " :
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, f$, i%
Set r = Intersect(Target, [S:S], UsedRange)
If Not r Is Nothing Then
    Application.EnableEvents = False
    For Each r In r
        f = r.Formula
        For i = 1 To Len(f)
            If InStr("=-+ ", Mid(f, i, 1)) = 0 Then r = Mid(f, i): Exit For
    Next i, r
    Application.EnableEvents = True
End If
End Sub
 

patricktoulon

XLDnaute Barbatruc
re
bonjour
puré on a jamais fini a toi hein
bon comme maintenant on peu maîtriser le numlok avec le sendkeys de l'application je l'utilise

pour l'interdiction de modifier le texte précédent voici un exemple

VB:
Dim activetarget
Dim activetargetValue
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Set Mask = ActiveSheet.Shapes("mask")
'If Not Mask Is Nothing Then Mask.Delete
    If Not Application.Intersect(Target, Range("s7:s20000")) Is Nothing Then
        Application.EnableEvents = False
        With Target
            .Value = Trim(Target.Value) & IIf(Right(Trim(Target.Value), 1) = "-", " ", " - ")
            If .Value = " - " Then .Value = ""
            Application.SendKeys ("{right " & Len(.Value) & "}")
            Application.SendKeys ""    'visiblement a pour effet d'annuler la touche precedente donc pas d'association de touche
            'qui ammene la plupart du temps a transformer les touches 4,6,2,8 en fleche et annule les autre du pavé
            'If .Value = "" Then

        End With
    End If
    Application.EnableEvents = True
    Set activetarget = Target: activetargetValue = Target.Value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = activetarget.Address Then
        If Left(Target.Value, Len(activetargetValue)) <> activetargetValue Then
            Application.Undo
            Target.Select
             End If
    End If
End Sub
testé!!! ;)

il va nous faire un excel 2030 celui là :p:p:p
 

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
Eh bien il en faudra un de plus car ma macro ne va pas si l'on entre 2 tirets séparés par un espace.

Il faut introduire l'espace dans la chaîne "=-+ " :
VB:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, f$, i%
Set r = Intersect(Target, [S:S], UsedRange)
If Not r Is Nothing Then
    Application.EnableEvents = False
    For Each r In r
        f = r.Formula
        For i = 1 To Len(f)
            If InStr("=-+ ", Mid(f, i, 1)) = 0 Then r = Mid(f, i): Exit For
    Next i, r
    Application.EnableEvents = True
End If
End Sub
Merci Gérard :)
 

Usine à gaz

XLDnaute Barbatruc
Supporter XLD
re
bonjour
puré on a jamais fini a toi hein
bon comme maintenant on peu maîtriser le numlok avec le sendkeys de l'application je l'utilise

pour l'interdiction de modifier le texte précédent voici un exemple

VB:
Dim activetarget
Dim activetargetValue
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Set Mask = ActiveSheet.Shapes("mask")
'If Not Mask Is Nothing Then Mask.Delete
    If Not Application.Intersect(Target, Range("s7:s20000")) Is Nothing Then
        Application.EnableEvents = False
        With Target
            .Value = Trim(Target.Value) & IIf(Right(Trim(Target.Value), 1) = "-", " ", " - ")
            If .Value = " - " Then .Value = ""
            Application.SendKeys ("{right " & Len(.Value) & "}")
            Application.SendKeys ""    'visiblement a pour effet d'annuler la touche precedente donc pas d'association de touche
            'qui ammene la plupart du temps a transformer les touches 4,6,2,8 en fleche et annule les autre du pavé
            'If .Value = "" Then

        End With
    End If
    Application.EnableEvents = True
    Set activetarget = Target: activetargetValue = Target.Value
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = activetarget.Address Then
        If Left(Target.Value, Len(activetargetValue)) <> activetargetValue Then
            Application.Undo
            Target.Select
             End If
    End If
End Sub
testé!!! ;)

il va nous faire un excel 2030 celui là :p:p:p
Merci Patrick :)
 

patricktoulon

XLDnaute Barbatruc
re
au propre ca donne ca
VB:
Dim activetarget As Range
Dim activetargetValue
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Application.Intersect(Target, Range("s7:s20000")) Is Nothing Then
        Application.EnableEvents = False
        With Target
            .Value = Trim(.Value) & IIf(Right(Trim(.Value), 1) = "-", " ", " - "): If .Value = " - " Then .Value = ""
            Application.SendKeys ("{right " & Len(.Value) + 1 & "}")
        End With
    End If
    Application.EnableEvents = True
    Set activetarget = Target: activetargetValue = Target.Value
    Application.SendKeys "" 'eraze (buffer keys) and (association keys) and send (null keycode)
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not activetarget Is Nothing Then
    If Target.Address = activetarget.Address Then
        If Left(Target.Value, Len(activetargetValue)) <> activetargetValue Then
            Application.Undo
            Target.Select
        End If
    End If
 End If
End Sub
 

jmfmarques

XLDnaute Accro
Ecrit près de 140 messages plus haut :
On y arrive peu à peu, n'est-ce-pas, Patrick ? :cool: Et (et tu le sais pourtant depuis au moins quelques mois), tant SendKeys que Application.Serndkeys peuvent avoir des effets collatéraux indésirables selon : 1) le clavier et 2) l'O.S. Ce n'est pas faute (de ma part), d'avoir tenté d'appeler...
Et ces effets "collatéraux" ne sont pas limités au "NUMLOCK" et peuvent sur certaines configurations clavier/OS concerner le "CAPSLOCK" également, notamment sur certains claviers sans pavé numérique.
J'en ai parlé plus haut.
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 185
Messages
2 086 012
Membres
103 093
dernier inscrit
Molinari