Formater une saisie de texte

re

très bonne remarque ou espace tout simplement

Bonjour curulis57, patrick, steelson,

Voici un nouvel essai, mais je rencontre encore des soucis :

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Or Target.Column <> 5 Then Exit Sub
    If Target.Value = "" Then Exit Sub    ' si vide on sort
    With Target
        .NumberFormat = "@"
        '
        Application.EnableEvents = False
        '
        If InStr(.Value, "+") > 0 Then
            If InStrRev(.Value, "+") = InStr(.Value, "+") Then
                If IsNumeric(Split(.Value, "+")(0)) And IsNumeric(Split(.Value, "+")(1)) Then
                    .Value = "F" & Format(Split(.Value, "+")(0), "0") & "-" & "F" & Format(Split(.Value, "+")(1), "0")
                    With .Characters(2, InStr(.Value, "-") - 2)
                        .Font.Subscript = True
                        .Font.Size = 12
                    End With
                    With .Characters(InStr(.Value, "-") + 2, Len(.Value) - InStr(.Value, "-") - 1)
                        .Font.Subscript = True
                        .Font.Size = 12
                    End With
                End If
            End If
        End If
        '
        Application.EnableEvents = True
        '
    End With
End Sub

Apres un premier essai concluant, le deuxième, on dirait que la macro ne fonctionne plus

macro desactivee

N'oublie pas que dans tes critères du as mis Target.Column <> 5 donc uniquement colonne E !

Et pourquoi ajouter .NumberFormat = "@" ? quel intérêt ?

on va peut être arrêter de se casser la tète et a globaliser ça

pas le limite de chiffre 2,3,etc....

mettre l'adress du range concerné dans le intersect

épisétoo!!!

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim T, x1&, x2&
    With Target
        T = .Text
        If InStr(1, T, "+") = 0 Or Target.Count > 1 Or Intersect([B:C], Target) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        T = Split(T, "+"): x1 = Len(T(0)): x2 = Len(T(1)): T = "F" & Join(T, "-F")
        Target.Value = T
        With .Characters(2, x1).Font: .Size = 12: .Subscript = True: End With
        With .Characters(x1 + 1 + 3, x2).Font: .Size = 12: .Subscript = True: End With
    End With
    Application.EnableEvents = True
End Sub

pas compliqué

Je n'ai pas compris ton intervention car la fonction proposée par curulis/atlas avec la modif du caractère séparateur et sans l'ajout du format n'a aucune limite du nombre de chiffres !

capture d ecran 269
3fsubscript.xlsm (15.40 Ko)

re

j'ai simplement réduit a essentiel les if et des machins et des trucs burk!!!

si "+" et si 1 cellule et si intersect(ou on veut)alors

t=split(t,+) x=len(t(0) : x=len(t(1) : t="F" & join(t,"-F")

retour a la cellule

subscript avec les x1 et x2 combinés

3 lignes !!!

Salut tout le monde,

moi, ce qui me dérange, c'est qu'un code laisse des failles à l'utilisateur lambda : burk !
La forme des données voulue par Atlas est F1-F23 (sauf qu'il n'y a pas Subscript dans les options de l'éditeur !) et rien d'autre.
Je t'invite donc à taper "5M+P24" ou "1+2+3" pour voir le feu d'artifice d'efficacité de ton code...

Mais, tu as raison sur un point auquel j'accorde moi-même beaucoup d'importance quitte à rendre la lecture du code quelque peu difficile pour un débutant : la concision !
Donc, économisons de l'espace et soyons efficace en... 1 ligne !

Private Sub Worksheet_Change(ByVal Target As Range)
'
If Target.Count = 1 Then If Target.Column = 5 And InStr(Target, "+") > 0 Then If InStrRev(Target, "+") = InStr(Target, "+") And IsNumeric(Split(Target, "+")(0)) And IsNumeric(Split(Target, "+")(1)) Then Target = "F" & Format(Split(Target, "+")(0), "0") & "-F" & Format(Split(Target, "+")(1), "0"): Target.Characters(2, InStr(Target, "-") - 2).Font.Size = 12: Target.Characters(2, InStr(Target, "-") - 2).Font.Subscript = True: Target.Characters(InStr(Target, "-") + 2, Len(Target) - (InStr(Target, "-") + 1)).Font.Size = 12: Target.Characters(InStr(Target, "-") + 2, Len(Target) - (InStr(Target, "-") + 1)).Font.Subscript = True Else Target = "": MsgBox "Valeur non conforme !" & Chr(10) & "Format exigé : 1+1", vbCritical + vbOKOnly, "ATLAS - Info" Else Exit Sub
'
End Sub

Bien le bonjour, Steelson !


A+

soit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim T, x1&, x2&
    With Target
        T = .Text
        If Not IsNumeric(Replace(T, "+", "")) Then Exit Sub
        If InStr(1, T, "+") = 0 Or Target.Count > 1 Or Intersect([B:C], Target) Is Nothing Or UBound(Split(T, "+")) > 1 Then Exit Sub
        Application.EnableEvents = False
        T = Split(T, "+"): x1 = Len(T(0)): x2 = Len(T(1)): T = "F" & Join(T, "-F")
        Target.Value = T
        With .Characters(2, x1).Font: .Size = 12: .Subscript = True: End With
        With .Characters(x1 + 1 + 3, x2).Font: .Size = 12: .Subscript = True: End With
    End With
    Application.EnableEvents = True
End Sub
Application.EnableEvents = False

pas utile

re

@Steelson

ben si !!!!tu le vois pas puisque la 2d fois la condition ne passe pas mais l'event est bien déclenché une 2d fois pour rien

c'est donc bien ce que je dis, si tu veux un code compact cela ne sert à rien

@Steelson réfléchi une 2 sec

d’après toi un event change qui se déclenche ça veut dire quoi?

d’après toi si le change se déclenche quoi d'autre se déclenche

c'est bon tu la vois la lumière

j'ai juste cru que tu voulais le faire en 3 lignes, c'est tout !

3 lignes !!!

le moteur oui 3 lignes

Merci pour vous tous

Rechercher des sujets similaires à "formater saisie texte"