IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

code vba excel


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    alger
    Inscrit en
    Novembre 2015
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : alger
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2015
    Messages : 34
    Par défaut code vba excel
    Bonjour tout le monde j’ai vraiment besoin de votre aide SVP je suis novice en vba
    J’ai un Userform avec une listbox multi colonne qui affiche les détails des clients, des textbox , combobox et trois bouton, je mit le code a votre disposition que j’ai utilisé il fonction bien, mon problème si avec la textbox1 « N° Client » elle ce rempli en chiffre 1,2,3… les N° Client, se que je veut que la textbox1 « N° Client » ce rempli de cette manière « CLT-1, CLT-2, CLT-3… » A chaque nouveau enregistrement, pour info j’ai une textbox que j’ai nommé Nbrligne elle affiche le nombre de ligne enregistré sur la feuil « Client » elle es en relation avec la textbox1
    Et merci d’avance
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    Option Compare Text
    Dim f, CL(), ListeVille(), LigneEnreg
     
    Private Sub B_nouveau_Click()
      razChampForm
      LigneEnreg = f.[A65000].End(xlUp).Row + 1
      Me.TextBox1 = f.Cells(LigneEnreg - 1, 1) + 1
      Me. Nbrligne = LigneEnreg
      Me.TextBox1.SetFocus
    End Sub
    Sub razChampForm()
     For Each k In Array(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12)
          Me("textbox" & k) = ""
     Next
     Me.ComboBox2 = ""
     Me.ComboBox3 = ""
    End Sub
     
     
    Private Sub TextBox13_Change()
    razChampForm
    On Error Resume Next
    With Sheets("Client").[A1].CurrentRegion
      .Parent.ShowAllData
      If TextBox13 <> "" Then .AutoFilter 2, TextBox13 & "*"
      If TextBox14 <> "" Then .AutoFilter 12, "*" & TextBox14 & "*"
      .Copy Feuil1.[A1] 'vers la feuille auxiliaire
      ListBox1.Clear
      With Feuil1.[A1].CurrentRegion
        ListBox1.List = .Offset(1).Resize(.Rows.Count - 1).Value
        .Clear 'RAZ
      End With
      .Parent.ShowAllData
    End With
    End Sub
     
    Private Sub TextBox14_Change()
    TextBox13_Change
    End Sub
     
    Private Sub UserForm_Initialize()
      Dim cw$
    cw = "20;50;50;90;50;50;50;50;50;50;50;50;40" 'largeurs à adapter
    ListBox1.ColumnWidths = cw
     
      Set f = Sheets("Client")
      If f.[B2] = "" Then Exit Sub
      CL = f.Range("a2:m" & [A65000].End(xlUp).Row).Value
      ListeVille = Range("villecodepostal").Value
      Me.ComboBox2.List = ListeVille
      Me.ComboBox3.List = Array("Bon", "Mauvais")
      For i = 1 To UBound(CL, 2) - 1
       largeur = largeur + f.Columns(i).Width * 1
      Next
      Me.ListBox1.List = CL 
     End Sub
     
    Private Sub ListBox1_Click()
      Ligne = ListBox1.ListIndex
      For Each i In Array(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12)
          Me("textbox" & i) = ListBox1.List(Ligne, i - 1)
      Next i
      Me.ComboBox2 = ListBox1.List(Ligne, 5)
       Me.ComboBox3 = ListBox1.List(Ligne, 12)
       reservation = Me.TextBox1
      Set result = f.[A:A].Find(what:=reservation)
      If Not result Is Nothing Then
        LigneEnreg = result.Row
        Me. Nbrligne = LigneEnreg
      Else
        MsgBox "Erreur no réservation"
      End If
    End Sub
     
    Private Sub ComboBox2_Change()
     On Error Resume Next
     If ActiveControl.Name <> "ComboBox2" Then Exit Sub
     On Error GoTo 0
     If Me.ComboBox2.ListIndex = -1 And _
         IsError(Application.Match(Me.ComboBox2, Application.Index(ListeVille, , 1), 0)) Then
         Dim b()
         Me.TextBox5 = ""
         clé = UCase(Me.ComboBox2) & "*"
         n = 0
         For i = LBound(ListeVille) To UBound(ListeVille)
           If UCase(ListeVille(i, 1)) Like clé Then
             n = n + 1: ReDim Preserve b(1 To 2, 1 To n)
             b(1, n) = ListeVille(i, 1): b(2, n) = ListeVille(i, 2)
           End If
          Next i
          If n > 0 Then
            ReDim Preserve b(1 To 2, 1 To n + 1)
            Me.ComboBox2.List = Application.Transpose(b)
            Me.ComboBox2.RemoveItem n
          End If
          Me.ComboBox2.DropDown
       Else
          On Error Resume Next
          Me.TextBox5 = Me.ComboBox2.Column(1)
       End If
    End Sub
     
    Private Sub B_suppression_Click()
     If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
      If LigneEnreg <> 0 Then
        Rows(LigneEnreg).Delete
        CL = f.Range("a2:m" & [A65000].End(xlUp).Row).Value
        TextBox13_Change
      End If
     End If
    End Sub
     
    Private Sub B_valider_Click()
      If Me.TextBox1 = "" Then
        MsgBox "Saisir un N° Client"
        Me.TextBox1.SetFocus
        Exit Sub
     End If
     If Not IsDate(Me.TextBox12) Then
        MsgBox "Saisir une Date!"
        Me.TextBox12.SetFocus
        Exit Sub
     End If
     If Me. Nbrligne <> 0 And Me.TextBox1 <> "" And LigneEnreg <> 0 Then
       lig = LigneEnreg
       For Each k In Array(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12)
         tmp = Me("textbox" & k)
         If IsNumeric(tmp) Then
            f.Cells(lig, k) = CDbl(tmp)
         Else
            If IsDate(f.Cells(lig, k)) Then
               f.Cells(lig, k) = CDate(tmp)
            Else
               f.Cells(lig, k) = tmp
            End If
         End If
       Next
       f.Cells(lig, 6) = Me.ComboBox2
       f.Cells(lig, 13) = Me.ComboBox3
       Ligne = ListBox1.ListIndex
       bd = f.Range("a2:m" & [A65000].End(xlUp).Row).Value
       TextBox13_Change
       Me.ListBox1.ListIndex = Ligne
       razChampForm
     End If
    End Sub

  2. #2
    Inactif  

    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2012
    Messages
    4 903
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : Canada

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2012
    Messages : 4 903
    Billets dans le blog
    36
    Par défaut
    Bonjour,

    S.V.P. Peut-être un petit tour par là, qui serait universellement apprécié pour faciliter la vie des gens qui vont devoir lire et comprendre ton code:

    http://club.developpez.com/aidenouve...es/Balises.gif

    Une chose que pourrais (devrais) faire, c'est de mettre toutes tes lignes On error en commentaires, un point d'arrêt avant ton TextBox qui cause problème et faire bout au pas-à-pas en regardant ta feuille Excel après chaque ligne. Des fois, c'est magique

  3. #3
    Membre averti
    Femme Profil pro
    alger
    Inscrit en
    Novembre 2015
    Messages
    34
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : alger
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Novembre 2015
    Messages : 34
    Par défaut
    ce code ne cause aucun problème
    je voudrais juste changé le remplissage de la textbox1 a chaque nouveau enregistrement qui es en chiffre par Clt-1,Clt-2 ...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    Option Compare Text
    Dim f, CL(), ListeVille(), LigneEnreg
     
    Private Sub B_nouveau_Click()
    razChampForm
    LigneEnreg = f.[A65000].End(xlUp).Row + 1
    Me.TextBox1 = f.Cells(LigneEnreg - 1, 1) + 1
    Me. Nbrligne = LigneEnreg
    Me.TextBox1.SetFocus
    End Sub
    Sub razChampForm()
    For Each k In Array(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12)
    Me("textbox" & k) = ""
    Next
    Me.ComboBox2 = ""
    Me.ComboBox3 = ""
    End Sub
     
     
    Private Sub TextBox13_Change()
    razChampForm
    On Error Resume Next
    With Sheets("Client").[A1].CurrentRegion
    .Parent.ShowAllData
    If TextBox13 <> "" Then .AutoFilter 2, TextBox13 & "*"
    If TextBox14 <> "" Then .AutoFilter 12, "*" & TextBox14 & "*"
    .Copy Feuil1.[A1] 'vers la feuille auxiliaire
    ListBox1.Clear
    With Feuil1.[A1].CurrentRegion
    ListBox1.List = .Offset(1).Resize(.Rows.Count - 1).Value
    .Clear 'RAZ
    End With
    .Parent.ShowAllData
    End With
    End Sub
     
    Private Sub TextBox14_Change()
    TextBox13_Change
    End Sub
     
    Private Sub UserForm_Initialize()
    Dim cw$
    cw = "20;50;50;90;50;50;50;50;50;50;50;50;40" 'largeurs à adapter
    ListBox1.ColumnWidths = cw
     
    Set f = Sheets("Client")
    If f.[B2] = "" Then Exit Sub
    CL = f.Range("a2:m" & [A65000].End(xlUp).Row).Value
    ListeVille = Range("villecodepostal").Value
    Me.ComboBox2.List = ListeVille
    Me.ComboBox3.List = Array("Bon", "Mauvais")
    For i = 1 To UBound(CL, 2) - 1
    largeur = largeur + f.Columns(i).Width * 1
    Next
    Me.ListBox1.List = CL
    End Sub
     
    Private Sub ListBox1_Click()
    Ligne = ListBox1.ListIndex
    For Each i In Array(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12)
    Me("textbox" & i) = ListBox1.List(Ligne, i - 1)
    Next i
    Me.ComboBox2 = ListBox1.List(Ligne, 5)
    Me.ComboBox3 = ListBox1.List(Ligne, 12)
    reservation = Me.TextBox1
    Set result = f.[A:A].Find(what:=reservation)
    If Not result Is Nothing Then
    LigneEnreg = result.Row
    Me. Nbrligne = LigneEnreg
    Else
    MsgBox "Erreur no réservation"
    End If
    End Sub
     
    Private Sub ComboBox2_Change()
    On Error Resume Next
    If ActiveControl.Name <> "ComboBox2" Then Exit Sub
    On Error GoTo 0
    If Me.ComboBox2.ListIndex = -1 And _
    IsError(Application.Match(Me.ComboBox2, Application.Index(ListeVille, , 1), 0)) Then
    Dim b()
    Me.TextBox5 = ""
    clé = UCase(Me.ComboBox2) & "*"
    n = 0
    For i = LBound(ListeVille) To UBound(ListeVille)
    If UCase(ListeVille(i, 1)) Like clé Then
    n = n + 1: ReDim Preserve b(1 To 2, 1 To n)
    b(1, n) = ListeVille(i, 1): b(2, n) = ListeVille(i, 2)
    End If
    Next i
    If n > 0 Then
    ReDim Preserve b(1 To 2, 1 To n + 1)
    Me.ComboBox2.List = Application.Transpose(b)
    Me.ComboBox2.RemoveItem n
    End If
    Me.ComboBox2.DropDown
    Else
    On Error Resume Next
    Me.TextBox5 = Me.ComboBox2.Column(1)
    End If
    End Sub
     
    Private Sub B_suppression_Click()
    If MsgBox("Etes vous sûr?", vbYesNo) = vbYes Then
    If LigneEnreg <> 0 Then
    Rows(LigneEnreg).Delete
    CL = f.Range("a2:m" & [A65000].End(xlUp).Row).Value
    TextBox13_Change
    End If
    End If
    End Sub
     
    Private Sub B_valider_Click()
    If Me.TextBox1 = "" Then
    MsgBox "Saisir un N° Client"
    Me.TextBox1.SetFocus
    Exit Sub
    End If
    If Not IsDate(Me.TextBox12) Then
    MsgBox "Saisir une Date!"
    Me.TextBox12.SetFocus
    Exit Sub
    End If
    If Me. Nbrligne <> 0 And Me.TextBox1 <> "" And LigneEnreg <> 0 Then
    lig = LigneEnreg
    For Each k In Array(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12)
    tmp = Me("textbox" & k)
    If IsNumeric(tmp) Then
    f.Cells(lig, k) = CDbl(tmp)
    Else
    If IsDate(f.Cells(lig, k)) Then
    f.Cells(lig, k) = CDate(tmp)
    Else
    f.Cells(lig, k) = tmp
    End If
    End If
    Next
    f.Cells(lig, 6) = Me.ComboBox2
    f.Cells(lig, 13) = Me.ComboBox3
    Ligne = ListBox1.ListIndex
    bd = f.Range("a2:m" & [A65000].End(xlUp).Row).Value
    TextBox13_Change
    Me.ListBox1.ListIndex = Ligne
    razChampForm
    End If
    End Sub

Discussions similaires

  1. [debutant] comment transposer mon code vba excel en access
    Par eclipse012 dans le forum VBA Access
    Réponses: 2
    Dernier message: 15/02/2008, 08h23
  2. Aide pour code VBA Excel
    Par NEC14 dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 01/02/2008, 09h33
  3. Aide pour simplifier un code VBA Excel
    Par NEC14 dans le forum Macros et VBA Excel
    Réponses: 19
    Dernier message: 24/01/2008, 16h15
  4. Code VBA excel form et modules
    Par Cptnikita dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 28/10/2007, 10h15
  5. Problème avec un code Vba Excel
    Par NEC14 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 23/10/2007, 16h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo