Microsoft 365 VBA - Traitement du texte d'une "zone de texte"

Rookiiz

XLDnaute Nouveau
Bonjour à tous,

Je créer actuellement un formulaire ou dans la première partie, on colle dans une zone de texte le détail d'une demande client.
Sauf que dans cette zone de texte (ou la propriété multi-ligne est activé), j'ai besoin d'identifier des éléments, qui vont alimenté des variables.

Petite photo pour illustrer.
Exemple_TextBox.png


Dans l'exemple, j'ai besoin d'extraire
- "Toto" dans une variable Nom
- "ROMO MARA" dans une variable Prénom
- "monsieurtoto.romo.mara@toto.com" dans une variable Email

Autre petite difficulté lors de la copie. Entre Nom et ROMO il y a une tabulation, et pas plusieurs espace.

Mon problème, c'est que traiter du texte dans des cellules je sais faire, mais dans une zone de texte, je suis un peu perdu.
Est-ce que c'est possible déjà ?

Merci d'avance a ceux qui prendrons le temps de me répondre.
Cordialement,
Rookiz
 
Solution
C
Bonjour le fil,

Pourquoi ne pas simplement traiter tous les aspects du textes 🤔
VB:
Private Sub Analyser_Click()
  Dim Ind As Integer, sTmp As String, Tab1() As String, Tab2()
  Dim sPrénom As String, sNom As String, eMail As String
  sTmp = Me.TB_Autotask.Value
  Tab1 = Split(sTmp, vbCr)
  For Ind = 0 To UBound(Tab1)
    If InStr(1, Tab1(Ind), "Prénom", vbTextCompare) > 0 Then
      sPrénom = Replace(Tab1(Ind), "Prénom ", "")
      sPrénom = Replace(sPrénom, vbCr, "")
    ElseIf InStr(1, Tab1(Ind), "Nom", vbTextCompare) > 0 Then
      sNom = Replace(Tab1(Ind), "Nom", "")
      ' Rétirer le caractère de tabulation
      sNom = Replace(sNom, vbTab, "")
      ' Retirer le caractère avant le nom
      sNom = Mid(sNom, 2, Len(sNom) - 1)...

job75

XLDnaute Barbatruc
Voici une solution qui fonctionne quel que soit le nombre de libellés à traiter.

Elle utilise la macro de tri bien connue Quick sort :
VB:
Private Sub Analyser_Click()
Dim b, a, x$, i%, pos%
b = Array("Nom", "Prénom", "Adresse email") 'liste à adapter
ReDim a(UBound(b))
x = Application.Trim(Replace(Replace(TextBox1, vbCrLf, ""), vbTab, ""))
For i = 0 To UBound(a)
    a(i) = InStr(x, b(i)) 'position du libellé
Next
tri a, b, 0, UBound(a) 'tri avec 2 tableaux
For i = 0 To UBound(a) - 1
    pos = a(i) + Len(b(i))
    a(i) = Trim(Mid(x, pos, a(i + 1) - pos))
Next
pos = a(i) + Len(b(i))
a(i) = Trim(Mid(x, pos, Len(x) + 1 - pos))
'---Vérification---
[A1:C1] = b 'libellés
[A2:C2] = a 'valeurs
End Sub

Sub tri(a, b, gauc, droi) ' Quick sort
Dim ref, g, d, temp
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
    Do While a(g) < ref: g = g + 1: Loop
    Do While ref < a(d): d = d - 1: Loop
    If g <= d Then
      temp = a(g): a(g) = a(d): a(d) = temp
      temp = b(g): b(g) = b(d): b(d) = temp
      g = g + 1: d = d - 1
    End If
Loop While g <= d
If g < droi Then Call tri(a, b, g, droi)
If gauc < d Then Call tri(a, b, gauc, d)
End Sub
 

Pièces jointes

  • TextBox(1).xlsm
    25.4 KB · Affichages: 3

laurent950

XLDnaute Accro
Bonsoir

Exemple en Poste #1
Votre Code en Poste #7
Private Sub Analyser_Click() texte = TB_Autotask.Value tableau = Split(texte, vbCrLf) MsgBox tableau(0) MsgBox tableau(1) tableau2 = Split(tableau(0), " ", 2) ResultatPrenom = tableau2(1) End Sub

Correction Ici
VB:
Private Type ZoneTexte
    Prénom As String
    Nom As String
    Mail As String
End Type
Private Sub CommandButton1_Click()
    Dim TraitementTexte As ZoneTexte
    TraitementTexte.Prénom = Trim(Split(Split(Application.Trim(TB_Autotask.Value), Chr(10))(0), " ")(1))
    TraitementTexte.Nom = Trim(Split(Split(Application.Trim(TB_Autotask.Value), Chr(10))(1), " ")(1))
    TraitementTexte.Mail = Trim(Split(Application.Trim(TB_Autotask.Value), Chr(10))(UBound(Split(Application.Trim(TB_Autotask.Value), Chr(10)))))
    MsgBox TraitementTexte.Prénom
    MsgBox TraitementTexte.Nom
    MsgBox TraitementTexte.Mail
End Sub

Une expression régulière serait pas mal ici Regex
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 115
Messages
2 085 443
Membres
102 889
dernier inscrit
monsef JABBOUR