erreur dans code formulaire

bredeche

XLDnaute Occasionnel
bonsoir

lors du lancement de mon userform et que je sélectionne la fiche
cela bug a la ligne "ComboBox2 = Ws.Cells(Ligne, "B")" je comprend pas
pouvez vous m'aider
merci par avance
Code:
Option Explicit

Dim Ws As Worksheet



'Pour le bouton Nouveau contact

Private Sub CommandButton1_Click()

    Dim L As Integer

    If MsgBox(" Confirmez-vous l’insertion de ce nouveau contact ? ", vbYesNo, " Demande de confirmation d’ajout ") = vbYes Then

        L = Sheets("Donné responsable").Range("a65536").End(xlUp).Row + 1 'Pour placer le nouvel enregistrement à la première ligne de tableau non vide

      Range("A" & L).Value = ComboBox1

        Range("B" & L).Value = ComboBox2

        Range("C" & L).Value = TextBoxSITE

        Range("D" & L).Value = TextBoxFONCTION

        Range("E" & L).Value = TextBoxTELFIXE

        Range("F" & L).Value = TextBoxTELMOB

        Range("G" & L).Value = TextBoxTELFAX

        Range("H" & L).Value = TextBoxEMAIL

      
    End If

End Sub
Private Sub ComboBox1_Change()

    Dim Ligne As Long

    Dim I As Integer

    If Me.ComboBox1.ListIndex = -1 Then Exit Sub

    Ligne = Me.ComboBox1.ListIndex + 2

    ComboBox2 = Ws.Cells(Ligne, "B")
   

    For I = 1 To 7

        Me.Controls(" TextBox " & I) = Ws.Cells(Ligne, I + 2)

    Next I

End Sub

'Pour le bouton Modifier

Private Sub CommandButton2_Click()

    Dim Ligne As Long

    Dim I As Integer

    If MsgBox(" Confirmez-vous la modification de ce contact ? ", vbYesNo, " Demande de confirmation de modification ") = vbYes Then

        If Me.ComboBox1.ListIndex = -1 Then Exit Sub

        Ligne = Me.ComboBox1.ListIndex + 2

        Ws.Cells(Ligne, " B ") = ComboBox2

        For I = 1 To 7

            If Me.Controls(" TextBox " & I).Visible = True Then

                Ws.Cells(Ligne, I + 2) = Me.Controls(" TextBox " & I)

            End If

        Next I

    End If

End Sub

Private Sub CommandButton3_Click()

    Unload Me

End Sub
 

Lone-wolf

XLDnaute Barbatruc
Bonsoir bredeche

Tu as mis en tête de Module la variable Ws, mais il faut l'initialiser dans Userform_Initialize, et les combobox aussi.

VB:
' Déclare la Variable L: As Long, Lg As long, Col As Long, I As Long ensuite

Private Sub UserForm_Initialize()
Dim I As Long, L as Long

Set Ws = Sheets("Donné responsable")

' Si il y a des doublons

With Ws
  For i = 2 To .Range("a" & Rows.Count).End(xlUp).Row
  ComboBox1 = .Range("a" & i)
  If ComboBox1.ListIndex = -1 Then ComboBox1.AddItem .Range("a" & i)
  Next i
End With

' Sinon

With Sheets("Donné responsable")
  L=  .Range("a" & Rows.Count).End(xlUp).Row
  ComboBox1.List = .Range("a2:a" & L)
End With
End Sub

With Ws
L = .Range("a" & Rows.Count).end(xlUp).Row + 1
.Range("a" & L) = Combobox1
Idem x fois
End With
End With

' Pourquoi tu à mis une boucle et 7 TexBox ( il y en a 6), alors que tu as renommé les Textbox:  _ TextBoxSITE -TextBoxFONCTION - TextBoxTELFIXE ??? 

For I = 1 To 7
        Me.Controls(" TextBox " & I) = Ws.Cells(Ligne, I + 2)
Next I

' Tu peux utiliser la boucle, mais il faut réinscrire le nom par défaut: TextBox1 - TextBox2 - TextBox3 etc.

' Pour le bouton 2 tu peux aussi faire comme ceci

With Ws
Lg = Application.Match(ComboBox1, .Columns(1), 0)
.Cells(Lg, 1) = Combobox1
.Cells(Lg, 2) = Combobox2
For Col = 3 To 8
.Cells(Lg, Col) = Controls("TextBox" & Col - 2)
End With
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
314 190
Messages
2 106 991
Membres
109 733
dernier inscrit
chardou