Condition recherche dans une macro

j'ai fait le code suivant qui plante encore merci d'avance pour votre correction ! qui correspond au bouton saisir un reglement dans ma page reglement :

Private Sub entrer_click()

   If Me.ComboBox1.ListIndex = -1 Then                      ' vérification des champs
      MsgBox "choisisez un type de reglement"
      Me.ComboBox1.SetFocus
      Exit Sub
    End If
       If Me.ComboBox2.ListIndex = -1 Then
      MsgBox "choisisez un type de document"
      Me.ComboBox2.SetFocus
      Exit Sub
    End If
        If Me.TextBox1.Value = "" Then
      MsgBox "saisir le numéro du document"
      Me.TextBox1.SetFocus
      Exit Sub
    End If
        If Me.ComboBox3.ListIndex = -1 Then
      MsgBox "choisisez un mode de reglement"
      Me.ComboBox3.SetFocus
      Exit Sub
    End If
        If Me.TextBox2.Value = "" Then
      MsgBox "choisisez la date du reglement"
      Me.TextBox2.SetFocus
      Exit Sub
    End If
    Application.ScreenUpdating = False
    If ComboBox2.Value = "bon de commande" Then
    Sheets("base_commande").Range("A65536").End(xlUp).Row 'recherche du numero de doc dans la base commande et enregistrement des données
 With TextBox1.Value  ' numero de doc a rechercher
        If ComboBox1.Value = "acompte 1" Then
        .Range("DS") = .Range(TextBox2.Value)
        .Range("DT") = .Range(ComboBox3.Value)
        .Range("DU") = .Range("oui")
        End If
        If ComboBox1.Value = "acompte 2" Then
        .Range("DV") = .Range(TextBox2.Value)
        .Range("DW") = .Range(ComboBox3.Value)
        .Range("DX") = .Range("oui")
        End If
        If ComboBox1.Value = "solde" Then
        .Range("DP") = .Range(TextBox2.Value)
        .Range("DQ") = .Range(ComboBox3.Value)
        .Range("DR") = .Range("oui")
        End If
    End With
    End If
    If ComboBox2.Value = "facture" Then
     Sheets("base_facture").Range("A65536").End(xlUp).Row 'recherche du numero de doc dans la base facture et enregistrement des données
    With TextBox1.Value 'numero doc a rechercher
        If ComboBox1.Value = "acompte 1" Then
        .Range("DS") = .Range(TextBox2.Value)
        .Range("DT") = .Range(ComboBox3.Value)
        .Range("DU") = .Range("oui")
        End If
        If ComboBox1.Value = "acompte 2" Then
        .Range("DV") = .Range(TextBox2.Value)
        .Range("DW") = .Range(ComboBox3.Value)
        .Range("DX") = .Range("oui")
        End If
        If ComboBox1.Value = "solde" Then
        .Range("DP") = .Range(TextBox2.Value)
        .Range("DQ") = .Range(ComboBox3.Value)
        .Range("DR") = .Range("oui")
        End If
    End With
    End If
    Unload Me

End Sub

le code s'arrête " With TextBox1.Value ' numero de doc a rechercher" ce que je cherche a faire c'est de remplire ma base dans la ligne qui correspond au n° de document dans une base donnée les case DS à DX

ci joint mon fichier

merci

15log-facture.xlsm (238.26 Ko)

Hello,

Ce qui ne joue pas dans ton code ce sont LES :

.Range("DS") et .Range(TextBox2.Value), ils ne font références à aucune feuille, ils font références au With TextBox1.Value.

Pas possible ! Range appartient à une feuille et pas à une TextBox.

Là les With TextBox1.Value ne servent à rien et les .Range font planter.

re , j'ai modiffier mon code comme suis mais je rencontre toujours une difficulté pour lui dire de copier le contenu de mes textbox et combobox dans ma base !

Private Sub entrer_click()

   If Me.ComboBox1.ListIndex = -1 Then                      ' vérification des champs
      MsgBox "choisisez un type de reglement"
      Me.ComboBox1.SetFocus
      Exit Sub
    End If
       If Me.ComboBox2.ListIndex = -1 Then
      MsgBox "choisisez un type de document"
      Me.ComboBox2.SetFocus
      Exit Sub
    End If
        If Me.TextBox1.Value = "" Then
      MsgBox "saisir le numéro du document"
      Me.TextBox1.SetFocus
      Exit Sub
    End If
        If Me.ComboBox3.ListIndex = -1 Then
      MsgBox "choisisez un mode de reglement"
      Me.ComboBox3.SetFocus
      Exit Sub
    End If
        If Me.TextBox2.Value = "" Then
      MsgBox "choisisez la date du reglement"
      Me.TextBox2.SetFocus
      Exit Sub
    End If
    Application.ScreenUpdating = False
    If Me.ComboBox2.Value = "bon de commande" Then
    Sheets("base_commande").Select
    i = Range("A65536").End(xlUp).Row 'recherche du numero de doc dans la base commande et enregistrement des données
 With Me.TextBox1.Value  ' numero de doc a rechercher
        If Me.ComboBox1.Value = "acompte 1" Then
        .Cells("DS", i) = Range("TextBox2.Value")
        .Cells("DT", i) = Range("ComboBox3.Value")
        .Cells("DU", i) = Range("oui")
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells("DV", i) = Range("TextBox2.Value")
        .Cells("DW", i) = Range("ComboBox3.Value")
        .Cells("DX", i) = Range("oui")
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells("DP", i) = Range("TextBox2.Value")
        .Cells("DQ", i) = Range("ComboBox3.Value")
        .Cells("DR", i) = Range("oui")
        End If
    End With
    End If
    If Me.ComboBox2.Value = "facture" Then
     Sheets("base_facture").Select
     i = Range("A65536").End(xlUp).Row 'recherche du numero de doc dans la base facture et enregistrement des données
    With Me.TextBox1.Value 'numero doc a rechercher
        If Me.ComboBox1.Value = "acompte 1" Then
        .Cells("DS", i) = Range("TextBox2.Value")
        .Cells("DT", i) = Range("ComboBox3.Value")
        .Cells("DU", i) = Range("oui")
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells("DV", i) = Range("TextBox2.Value")
        .Cells("DW", i) = Range("ComboBox3.Value")
        .Cells("DX", i) = Range("oui")
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells("DP", i) = Range("TextBox2.Value")
        .Cells("DQ", i) = Range("ComboBox3.Value")
        .Cells("DR", i) = Range("oui")
        End If
    End With
    End If
    Unload Me

End Sub

par quoi je peut remplacer Range("textbox2.value") et pour que dans la case ("DR", i) il me marque oui est ce que j'ai bon.

merci

Re,

Range ou Cells c'est pareil, les deux appartiennent à une feuille !

Ça devrait être un truc du genre... En supprimant le With TextBox1.Value...

        If ComboBox1.Value = "acompte 1" Then
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DS") = TextBox2.Value
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DT") = ComboBox3.Value
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DU") = Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("oui")
        End If
        If ComboBox1.Value = "acompte 2" Then
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DV") = TextBox2.Value
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DW") = ComboBox3.Value
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DX") = Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("oui")
        End If
        If ComboBox1.Value = "solde" Then
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DP") = TextBox2.Value
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DQ") = ComboBox3.Value
        Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("DR") = Sheets("NOM_DE_LA_FEUILLE_VOULUE").Range("oui")
        End If

Ou mieux encore et plus lisible...

    With Sheets("NOM_DE_LA_FEUILLE_VOULUE")
        If ComboBox1.Value = "acompte 1" Then
            .Range("DS") = TextBox2.Value
            .Range("DT") = ComboBox3.Value
            .Range("DU") = .Range("oui")
        End If
        If ComboBox1.Value = "acompte 2" Then
            .Range("DV") = TextBox2.Value
            .Range("DW") = ComboBox3.Value
            .Range("DX") = .Range("oui")
        End If
        If ComboBox1.Value = "solde" Then
            .Range("DP") = TextBox2.Value
            .Range("DQ") = ComboBox3.Value
            .Range("DR") = .Range("oui")
        End If
    End With

Là oui les .Range et/ou .Cells appartiennent à la feuille "NOM_DE_LA_FEUILLE_VOULUE"

re si j'ai ecrit au depart with textbox1.value c'est pour celectionner la ligne dans ma base qui correspond a mon numero de document, apparament j'ais tous faut donc si je mais with sheets("base_commande") a quel moment je lui dit de comparer les n° trouver dans la colonne A et mon n° de ma textboxe1 pour ecrire dans cette ligne la les données a ajouter et pas dans une autre ?

merci

re j'ai corriger mon code et il s'arrête toujours a :

.Cells("DS", j & i) = TextBox2.Value

le code corriger donne ceci :

 Application.ScreenUpdating = False
    If Me.ComboBox2.Value = "bon de commande" Then
     With Sheets("base_commande")
     j = Me.TextBox1.Value
     i = Range("A65536").End(xlUp).Row  ' numero de doc a rechercher
     If Me.TextBox1.Value = False Then
    MsgBox "le numero de bon de commande n'existe pas, ou est incomplet veuillez le re-saisir merci"
    Me.TextBox2.SetFocus
    Exit Sub
    End If
        If Me.ComboBox1.Value = "acompte 1" Then
        .Cells("DS", j & i) = TextBox2.Value
        .Cells("DT", j & i) = ComboBox3.value
        .Cells("DU", j & i) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells("DV", j & i) = TextBox2.Value
        .Cells("DW", j & i) = ComboBox3.Value
        .Cells("DX", j & i) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells("DP", j & i) = TextBox2.Value
        .Cells("DQ", j & i) = ComboBox3.Value
        .Cells("DR", j & i) = .Range("oui")
        End If
    End With
    End If

   If Me.ComboBox2.Value = "facture" Then
    With Sheets("base_facture") 'numero doc a rechercher
    j = Me.TextBox1.Value
    i = Range("A65536").End(xlUp).Row
    If Me.TextBox1.Value = False Then
    MsgBox "le numero de bon de commande n'existe pas, ou est incomplet veuillez le re-saisir merci"
    Me.TextBox2.SetFocus
    Exit Sub
    End If
        If Me.ComboBox1.Value = "acompte 1" Then
        .Cells("DS", j & i) = TextBox2.Value
        .Cells("DT", j & i) = ComboBox3.value
        .Cells("DU", j & i) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells("DV", j & i) = TextBox2.Value
        .Cells("DW", j & i) = ComboBox3.Value
        .Cells("DX", j & i) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells("DP", j & i) = TextBox2.Value
        .Cells("DQ", j & i) = ComboBox3.Value
        .Cells("DR", j & i) = .Range("oui")
        End If
    End With
    End If
    Unload Me

End Sub

merci de me corriger

Re,

Inspire-toi de ceci

Private Sub CommandButton1_Click()

    Dim lign, i As Integer

    On Error Resume Next

    With Sheets("Feuil1")
        For lign = 2 To .Range("A1000").End(xlUp).Row
            If TextBox1.Text = .Cells(lign, 1).Value Then
                .Cells(lign, 1).Find(What:=TextBox1.Text, After:=.Cells(lign, 1), LookIn:=xlFormulas, LookAt _
                    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                    False, SearchFormat:=False).Activate
                MsgBox "la valeur se trouve dans la cellule : " & .Cells(lign, 1).Address
                Exit Sub
            End If
        Next lign
    End With

End Sub

avez vous une corde ! car je vais finir par perdre la tête !

si dans ma page reglement avec le formulaire reglement1 (cible du bouton saisi des reglement) je saisi les donnée suivante :

choix du reglement = acompte 1

type de document = bon de commande

n° du document =2013-00005 (numero present dans ma base_commande)

mode de reglement = chèque

date = date du jours

je plante a .Cells("DS", j & i) = TextBox2.Text

et j'ai beau essayer de modiffier mon code encore et encore rien n'y fait j'en appelle a une ame charitable pour me donner un coup de pouce

merci d'avance

ci_joint la dernière version de mon fichier

9log-facture.xlsm (241.68 Ko)

percévérent je le suis mais je ne comprend pas le pourquoi.

j'ai encore refait mon code et il plante toujours a:

.Cells(j & i, 123) = Me.TextBox2.Value

et pourtant quand il passe en mode debogage et que je survole les donneés de mon code avec la souris je vois que les valeurs réccupérées sont bien les bonnes ! j'ai donc pensé que cela pouvait venir des paramettres des celules de mes base car j'avais attribuer le format date au célule destinées a recevoir la date mais non j'ai remis en standar et cela ne change rien il se refuse a ecrire dans les case de ma base comme si il ne voulait pas mettre a jour la ligne concerné

auriez vous la bonté de vous pencher sur mon problème.

mon code réecrit :

Private Sub entrer_click()

   If Me.ComboBox1.ListIndex = -1 Then                      ' vérification des champs
      MsgBox "choisisez un type de reglement"
      Me.ComboBox1.SetFocus
      Exit Sub
    End If
       If Me.ComboBox2.ListIndex = -1 Then
      MsgBox "choisisez un type de document"
      Me.ComboBox2.SetFocus
      Exit Sub
    End If
        If Me.TextBox1.Value = "" Then
      MsgBox "saisir le numéro du document"
      Me.TextBox1.SetFocus
      Exit Sub
    End If
        If Me.ComboBox3.ListIndex = -1 Then
      MsgBox "choisisez un mode de reglement"
      Me.ComboBox3.SetFocus
      Exit Sub
    End If
        If Me.TextBox2.Value = "" Then
      MsgBox "choisisez la date du reglement"
      Me.TextBox2.SetFocus
      Exit Sub
    End If

    Application.ScreenUpdating = False
    Dim i As Integer, j As String
    If Me.ComboBox2.Value = "bon de commande" Then
    j = Me.TextBox1.Value

    With Sheets("base_commande").Select
    i = Sheets("base_commande").Range("A65536").End(xlUp).Row 'numero doc a rechercher
        If Me.ComboBox1.Value = "acompte 1" Then
        .Cells(j & i, 123) = Me.TextBox2.Value
        .Cells(j & i, 124) = Me.ComboBox3.Value
        .Cells(j & i, 125) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells(j & i, 126) = Me.TextBox2.Value
        .Cells(j & i, 127) = Me.ComboBox3.Value
        .Cells(j & i, 128) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells(j & i, 120) = Me.TextBox2.Value
        .Cells(j & i, 121) = Me.ComboBox3.Value
        .Cells(j & i, 122) = .Range("oui")
        End If
    End With
    End If

   If Me.ComboBox2.Value = "facture" Then

    j = Me.TextBox1.Value

    With Sheets("base_facture").Select
    i = Sheets("base_facture").Range("A65536").End(xlUp).Row 'numero doc a rechercher
        If Me.ComboBox1.Value = "acompte 1" Then
        .Cells(j & i, 123) = Me.TextBox2.Value
        .Cells(j & i, 124) = Me.ComboBox3.Value
        .Cells(j & i, 125) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells(j & i, 126) = Me.TextBox2.Value
        .Cells(j & i, 127) = Me.ComboBox3.Value
        .Cells(j & i, 128) = .Range("oui")
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells(j & i, 120) = Me.TextBox2.Value
        .Cells(j & i, 121) = Me.ComboBox3.Value
        .Cells(j & i, 122) = .Range("oui")
        End If
    End With
    End If
    Unload Me

End Sub

Bonjour

Penches toi du côte de J & I, cela ne veut pas dire grand chose

Tu dois avoir un numéro de ligne

I est le numéro de la dernière ligne de ta base, J est ????????

Donc cela doit être J + I (mais pas sur que tu obtiennes la ligne que tu veux)

effectivement avec tous mes essai je vient de m'apercevoir que i est la dernière ligne mais comment ecrire pour que i soit la ligne dons la valeur presente dans la colonne A est j (numero saisi dans ma textbox1

merci

Bonjour

Il faut chercher cette valeur

style (fait à main levée - attention aux erreurs éventuelles)

Dim Cel As Range
'.
'.
'.
With Sheets("base_commande")
  Set Cel = .Columns("A").Find(What:=Me.Me.TextBox1, Lookin:=xlValues, Lookat:=xlWhole)
  If Not Cel is Nothing Then
    ' numéro existe
    i = Cel.row
  Else
    ' Numéro n'existe pas donc on écrit à la fin
    i = .Range("A65536").End(xlUp).Row + 1
  End If
  .Cells(I, 123)= Me.TextBox2
  ' etc......

je viens de regarder tes conseils que je mettrais en appliction des demain car il ce fait tard et depuis ce matin que je tourne en rond avec ce bout de code (comme on dit la nuit porte conseil et surtout repose les neurones )

bonne nuit et je vous tiens au courant de l'avancement

au plaisir de vous lire

la nuit a portée conseil et les indication de banzai64 mon permis au petit matin de venir a bout de ce code qui fonctionne bien maintenant. ces indication mon permis de corriger d'autre code (de les simplifier) et tous marche. mais si je peut abuser dans mon code j'ai integrer si le n° n'existe pas une msgbox puis un setfocus, mais comment faire pour que lors du setfocus le curseur ne soit pas en fin de ligne dans ma textbox mais que le contenu soit selectionné c'est juste pour que cla fasse plus fun sinon cela n'est pas d'un imperatif absolue

sinon pour que ceux qui cherche puis etre inspirés voici le code qui fonctionne :

Private Sub entrer_click()

   If Me.ComboBox1.ListIndex = -1 Then                      ' vérification des champs
      MsgBox "choisisez un type de reglement"
      Me.ComboBox1.SetFocus
      Exit Sub
    End If
       If Me.ComboBox2.ListIndex = -1 Then
      MsgBox "choisisez un type de document"
      Me.ComboBox2.SetFocus
      Exit Sub
    End If
        If Me.TextBox1.Value = "" Then
      MsgBox "saisir le numéro du document"
      Me.TextBox1.SetFocus
      Exit Sub
    End If
        If Me.ComboBox3.ListIndex = -1 Then
      MsgBox "choisisez un mode de reglement"
      Me.ComboBox3.SetFocus
      Exit Sub
    End If
        If Me.TextBox2.Value = "" Then
      MsgBox "choisisez la date du reglement"
      Me.TextBox2.SetFocus
      Exit Sub
    End If
Application.ScreenUpdating = False
    Dim i As Integer, j As String
    Dim cel As Range
    If Me.ComboBox2.Value = "bon de commande" Then
With Sheets("base_commande")
  Set cel = .Columns("A").Find(What:=Me.TextBox1, LookIn:=xlValues, Lookat:=xlWhole)
  If Not cel Is Nothing Then
  i = cel.Row
    Else
      MsgBox "le numero de bon de commande n'existe pas, ou est incomplet veuillez le re-saisir merci"
    Me.TextBox1.SetFocus
    Exit Sub
End If
    If Me.ComboBox1.Value = "acompte 1" Then
        .Cells(i, 123) = Me.TextBox2.Value
        .Cells(i, 124) = Me.ComboBox3.Value
        .Cells(i, 125) = "oui"
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells(i, 126) = Me.TextBox2.Value
        .Cells(i, 127) = Me.ComboBox3.Value
        .Cells(i, 128) = "oui"
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells(i, 120) = Me.TextBox2.Value
        .Cells(i, 121) = Me.ComboBox3.Value
        .Cells(i, 122) = "oui"
        End If
    End With
End If
   If Me.ComboBox2.Value = "facture" Then
With Sheets("base_facture")
  Set cel = .Columns("A").Find(What:=Me.TextBox1, LookIn:=xlValues, Lookat:=xlWhole)
  If Not cel Is Nothing Then
  i = cel.Row
    Else
      MsgBox "le numero de bon de commande n'existe pas, ou est incomplet veuillez le re-saisir merci"
    Me.TextBox1.SetFocus
    Exit Sub
 End If
    If Me.ComboBox1.Value = "acompte 1" Then
        .Cells(i, 123) = Me.TextBox2.Value
        .Cells(i, 124) = Me.ComboBox3.Value
        .Cells(i, 125) = "oui"
        End If
        If Me.ComboBox1.Value = "acompte 2" Then
        .Cells(i, 126) = Me.TextBox2.Value
        .Cells(i, 127) = Me.ComboBox3.Value
        .Cells(i, 128) = "oui"
        End If
        If Me.ComboBox1.Value = "solde" Then
        .Cells(i, 120) = Me.TextBox2.Value
        .Cells(i, 121) = Me.ComboBox3.Value
        .Cells(i, 122) = "oui"
        End If
   End With
End If
Unload Me
End Sub

encore merci , pour rire je range la corde que j'ai autour du coup, dans le tiroir car je suis sur qu'excel me donneras d'autre occasion pour s'en servir !

Slt tout le monde,

Désolé ai pas pu te répondre, ou plutôt j'ai répondu, mais ai du me chiquer puisque je vois que le message n'est pas passé.

Cela dit, je suis content que l'ami Banzaï aille pu te répondre et t'aider.

C'est à peu près ce que je voulais aussi te dire et aussi faire un petit rappel sur Cells(ligne, colonne).

Bref pour ce qui est du focus sur le TextBox avec la sélection du contenu, voici ces deux lignes à mettre avant les SetFocus...

TextBox1.SelStart = 0

TextBox1.SelLength = Len(TextBox1.Text)

TextBox1.SetFocus

Rechercher des sujets similaires à "condition recherche macro"