VBA: remplir un userForm à partir d'Excel
kdison13
Messages postés
15
Statut
Membre
-
kdison13 Messages postés 15 Statut Membre -
kdison13 Messages postés 15 Statut Membre -
Bonjour,
j'ai un soucis de programmation! J'ai un fichier contenant 17 UserForm qui permettent de remplir des feuilles Excel (1 numéro de dossier = 1 ligne Excel). A l'inverse, j'aimerais que l'utilisateur puisse modifier ses données et donc pouvoir naviguer dans les différents userForm, qui seraient remplis par les infos souhaitées(sachant qu'une ligne Excel correspond à 1 numéro de dossier), et les modifier.
Je pensais avoir programmé correctement, mais les userForm ne se remplissent pas! Pourtant j'ai bien l'impression que les éléments du UserForm arrive à récupérer la valeur voulue, mais ne l'affiche pas! Voici la fonction (la première sur les 17) à laquelle mon code fait appel pour récupérer ces valeurs:
Si une âme charitable peut m'aider, ça serait vraiment super!!
Merci d'avance
j'ai un soucis de programmation! J'ai un fichier contenant 17 UserForm qui permettent de remplir des feuilles Excel (1 numéro de dossier = 1 ligne Excel). A l'inverse, j'aimerais que l'utilisateur puisse modifier ses données et donc pouvoir naviguer dans les différents userForm, qui seraient remplis par les infos souhaitées(sachant qu'une ligne Excel correspond à 1 numéro de dossier), et les modifier.
Je pensais avoir programmé correctement, mais les userForm ne se remplissent pas! Pourtant j'ai bien l'impression que les éléments du UserForm arrive à récupérer la valeur voulue, mais ne l'affiche pas! Voici la fonction (la première sur les 17) à laquelle mon code fait appel pour récupérer ces valeurs:
Public Function recupDossier(numDossier As String)
Dim ligne As Integer
Dim i As Integer
i = 1
ligne = -1
With Sheets("patient")
Do While (.Range("A" & i) <> numDossier And .Range("A" & i) <> Empty)
i = i + 1
Loop
If (.Range("A" & i).Value = numDossier) Then
ligne = i
End If
End With
'MsgBox "Numéro de ligne du dossier : " & ligne
If (ligne > 0) Then
With Sheets("patient")
Me.TextInitial.Value = Range("B" & ligne).Value
Me.TextDate.Value = Range("C" & ligne).Value
Me.TextExam.Value = Range("D" & ligne).Value
Me.TextNaissance.Value = Range("E" & ligne).Value
Me.TextResid.Value = Range("F" & ligne).Value
If Range("G" & ligne).Value = Me.OptionCelib.Caption Then Me.OptionCelib.Value = True
If Range("G" & ligne).Value = Me.OptionMarie.Caption Then Me.OptionMarie.Value = True
If Range("G" & ligne).Value = Me.OptionDivorce.Caption Then Me.OptionDivorce.Value = True
If Range("G" & ligne).Value = Me.OptionVeuf.Caption Then Me.OptionVeuf.Value = True
If Range("G" & ligne).Value = Me.OptionCouple.Caption Then Me.OptionCouple.Value = True
If Range("H" & ligne).Value = Me.Optionnb_0.Caption Then Me.Optionnb_0.Value = True
If Range("H" & ligne).Value = Me.Optionnb_1.Caption Then Me.Optionnb_1.Value = True
If Range("H" & ligne).Value = Me.Optionnb_2.Caption Then Me.Optionnb_2.Value = True
If Range("H" & ligne).Value = Me.Optionnb_3.Caption Then Me.Optionnb_3.Value = True
If Range("H" & ligne).Value = Me.Optionnb_4.Caption Then Me.Optionnb_4.Value = True
If Range("H" & ligne).Value = Me.Optionnb_5.Caption Then Me.Optionnb_5.Value = True
If Range("H" & ligne).Value = Me.Optionnb_sup5.Caption Then Me.Optionnb_sup5.Value = True
Me.TextAge_enfants.Value = Range("I" & ligne).Value
Me.TextProf.Value = Range("J" & ligne).Value
Me.Textrev_foy.Value = Range("K" & ligne).Value
If Range("L" & ligne).Value = Me.OptionNormal.Caption Then Me.OptionNormal.Value = True
If Range("L" & ligne).Value = Me.OptionConfort.Caption Then Me.OptionConfort.Value = True
If Range("L" & ligne).Value = Me.OptionConfort.Caption Then Me.OptionConfort.Value = True
If Range("M" & ligne).Value = Me.OptionTUT_OUI.Caption Then Me.OptionTUT_OUI.Value = True
If Range("M" & ligne).Value = Me.OptionTUT_NON.Caption Then Me.OptionTUT_NON.Value = True
Me.TextTUT_ANNEE.Value = Range("N" & ligne).Value
End With
End If
End Function
Si une âme charitable peut m'aider, ça serait vraiment super!!
Merci d'avance
12 réponses
-
Bonjour,
Ce n'est peut-être pas çà mais tu a oublié les points devant les range (ca, je sais bien faire!!!) donc si la feuille active n'est pas "patient"==>moment de solitude
With Sheets("patient") Me.TextInitial.Value = .Range("B" & ligne).Value
-
-
Merci les gars!! effectivement ça m'a l'air de mieux fonctionner comme ça, mais il me reste un soucis: ça ne fonctionne que pour le 1er UserForm!
Je ne peux vous mettre tout mon fichier en pièce jointe, donc je vous explique rapidement:
lorsque j'appuie sur un bouton 'SUIVANT' ou 'PRECEDENT' (sur les UserForm)je fais appel à cette fonction:Public Function showUserForm(numDossier As String, index As Integer, recup As Boolean) Dim Form Select Case index Case 1: form1.clearContent If numDossier <> " " Then form1.TextSUBJID.Value = numDossier If (recup = True) Then ret = form1.recupDossier(numDossier:=numDossier) End If form1.Show Else form1.Show End If Case 2: form2.clearContent If numDossier <> " " Then form2.TextSUBJID.Value = numDossier If (recup = True) Then ret = form2.recupDossier(numDossier:=numDossier) End If form2.Show Else form2.TextSUBJID.Value = numDossier form2.Show End If etc ...
qui fait elle appel aux fonctions 'recupDossier' dont je vous ai montré le code dans mon premier post!
Si vous voyez ce qui peut clocher, je suis preneuse !!!!
Merci d'avance! -
Lorsque tu mets plusieurs communautés à ton service, préviens les différents forums et indique tout de suite qu'une solution a été trouvée et laquelle;
Actuellement sur DVP, il y ya certainement des forumeurs qui cherchent une solution sur le coup du .range
cette attitude est très mal vue des forumeurs ; bénévoles= domestiques ?
Donc, prend des dispositions si tu ne tiens pas à être grillé sur l'ensemble des forums -
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question -
Bonjour,
tu pourrais remplacer..If Range("G" & ligne).Value = Me.OptionCelib.Caption Then Me.OptionCelib.Value = True
par en plus cour,Me.OptionCelib.Value = Range("G" & ligne).Value = OptionCelib.CaptionLe "Me" n'est pas nécessaire.
Et 17 Userform ?? y sont tous les même ?
A+ -
Bonjour tout le monde!!
ça y est j'arrive aux bout de mes problèmes! Youpi ! :-D
MAIS il m'en reste un que je ne comprends pas du tout! Toutes mes données se récupèrent bien, i.e. les userForm sont bien remplis avec les bonnes données récupérées dans Excel, SAUF pour les cases à cocher (CheckBox) et zones de texte (TextBox) des UserForm 8, 9, 11, 12 et 13 uniquement!
J'ai comparé la programmation avec celle que j'ai fait pour les UserForm qui fonctionnent et je ne vois pas ce qui peut poser problème!!
fonction 'recupDossier' qui fonctionne pour le userform 5:Public Function recupDossier(numDossier As String) Dim ligne As Integer Dim i As Integer i = 1 ligne = -1 With Sheets("maladie patient") Do While (Sheets("patient").Range("A" & i) <> numDossier And Sheets("patient").Range("A" & i) <> Empty) i = i + 1 Loop If (Sheets("patient").Range("A" & i).Value = numDossier) Then ligne = i End If End With 'MsgBox "Numéro de ligne du dossier : " & ligne If (ligne > 0) Then With Sheets("maladie patient") 'a quelle date le cancer de votre proche a-t-il été diagnostiqué ? If Sheets("maladie patient").Range("B" & ligne).Value = Me.Optioncancer1.Caption Then Me.Optioncancer1.Value = True If Sheets("maladie patient").Range("B" & ligne).Value = Me.Optioncancer2.Caption Then Me.Optioncancer2.Value = True 'est-ce que le cancer de votre proche a été un choc pour vous ? If Sheets("maladie patient").Range("C" & ligne).Value = Me.OptionCHOC1.Caption Then Me.OptionCHOC1.Value = True If Sheets("maladie patient").Range("C" & ligne).Value = Me.OptionCHOC2.Caption Then Me.OptionCHOC2.Value = True 'si oui à quel moment ? If Sheets("maladie patient").Range("D" & ligne).Value = Me.OptionMOM1.Caption Then Me.OptionMOM1.Value = True If Sheets("maladie patient").Range("D" & ligne).Value = Me.OptionMOM2.Caption Then Me.OptionMOM2.Value = True If Sheets("maladie patient").Range("D" & ligne).Value = Me.OptionMOM3.Caption Then Me.OptionMOM3.Value = True 'précisez If Sheets("maladie patient").Range("E" & ligne).Value = Me.OptionEF_TT1.Caption Then Me.OptionEF_TT1.Value = True If Sheets("maladie patient").Range("E" & ligne).Value = Me.OptionEF_TT2.Caption Then Me.OptionEF_TT2.Value = True If Sheets("maladie patient").Range("E" & ligne).Value = Me.OptionEF_TT3.Caption Then Me.OptionEF_TT3.Value = True If Sheets("maladie patient").Range("E" & ligne).Value = Me.OptionEF_TT4.Caption Then Me.OptionEF_TT4.Value = True If Sheets("maladie patient").Range("E" & ligne).Value = Me.OptionEF_TT5.Caption Then Me.OptionEF_TT5.Value = True If Sheets("maladie patient").Range("E" & ligne).Value = Me.OptionEF_TT6.Caption Then Me.OptionEF_TT6.Value = True 'si autre, précisez Me.TextPREC1.Value = Sheets("maladie patient").Range("F" & ligne).Value 'depuis la maladie de votre proche, ressentez-vous un état de fatigue ? If Sheets("maladie patient").Range("G" & ligne).Value = Me.OptionFAT1.Caption Then Me.OptionFAT1.Value = True If Sheets("maladie patient").Range("G" & ligne).Value = Me.OptionFAT2.Caption Then Me.OptionFAT2.Value = True 'si oui, situez votre niveau de fatigue: If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF1.Caption Then Me.OptionNIVF1.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF2.Caption Then Me.OptionNIVF2.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF3.Caption Then Me.OptionNIVF3.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF4.Caption Then Me.OptionNIVF4.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF5.Caption Then Me.OptionNIVF5.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF6.Caption Then Me.OptionNIVF6.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF7.Caption Then Me.OptionNIVF7.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF8.Caption Then Me.OptionNIVF8.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF9.Caption Then Me.OptionNIVF9.Value = True If Sheets("maladie patient").Range("H" & ligne).Value = Me.OptionNIVF10.Caption Then Me.OptionNIVF10.Value = True 'commentaire: Me.TextComFAT.Value = Sheets("maladie patient").Range("I" & ligne).Value 'accompagnez-vous votre proche lors des rendez-vous médicaux ? If Sheets("maladie patient").Range("J" & ligne).Value = Me.OptionRDV1.Caption Then Me.OptionRDV1.Value = True If Sheets("maladie patient").Range("J" & ligne).Value = Me.OptionRDV2.Caption Then Me.OptionRDV2.Value = True 'si oui précisez: If Sheets("maladie patient").Range("K" & ligne).Value = Me.OptionRDVOU1.Caption Then Me.OptionRDVOU1.Value = True If Sheets("maladie patient").Range("K" & ligne).Value = Me.OptionRDVOU2.Caption Then Me.OptionRDVOU2.Value = True If Sheets("maladie patient").Range("K" & ligne).Value = Me.OptionRDVOU3.Caption Then Me.OptionRDVOU3.Value = True 'lesquels 'consultations If Sheets("maladie patient").Range("L" & ligne).Value = "OUI" Then Me.CheckBox1.Value = True 'examens If Sheets("maladie patient").Range("M" & ligne).Value = "OUI" Then Me.CheckBox2.Value = True 'hospitalisations If Sheets("maladie patient").Range("N" & ligne).Value = "OUI" Then Me.CheckBox3.Value = True 'en cas d'hospitalisation, êtes-vous If Sheets("maladie patient").Range("O" & ligne).Value = Me.OptionETHOS1.Caption Then Me.OptionETHOS1.Value = True If Sheets("maladie patient").Range("O" & ligne).Value = Me.OptionETHOS2.Caption Then Me.OptionETHOS2.Value = True If Sheets("maladie patient").Range("O" & ligne).Value = Me.OptionETHOS3.Caption Then Me.OptionETHOS3.Value = True 'autre précisez Me.TextHOSCOM.Value = Sheets("maladie patient").Range("P" & ligne).Value 'commentaires Me.TextHOSPCOM.Value = Sheets("maladie patient").Range("Q" & ligne).Value End With End If End Function
fonction 'recupDossier' qui ne fonctionne pas pour le userform 8:Public Function recupDossier(numDossier As String) Dim ligne As Integer Dim i As Integer i = 1 ligne = -1 With Sheets("organisation au domicile") Do While (Sheets("patient").Range("A" & i) <> numDossier And Sheets("patient").Range("A" & i) <> Empty) i = i + 1 Loop If (Sheets("patient").Range("A" & i).Value = numDossier) Then ligne = i End If End With 'MsgBox "Numéro de ligne du dossier : " & ligne If (ligne > 0) Then With Sheets("organisation au domicile") 'aidez-vous votre proche depuis la maladie ? If Sheets("organisation au domicile").Range("B" & ligne).Value = Me.OptionAID1.Caption Then Me.OptionAID1.Value = True If Sheets("organisation au domicile").Range("B" & ligne).Value = Me.OptionAID2.Caption Then Me.OptionAID2.Value = True 'si oui participez-vous : 'aux soins médicaux If Sheets("organisation au domicile").Range("C" & ligne).Value = "OUI" Then Me.CheckBox1.Value = True 'aux soins corporels If Sheets("organisation au domicile").Range("D" & ligne).Value = "OUI" Then Me.CheckBox2.Value = True 'au soutien affectif et moral If Sheets("organisation au domicile").Range("E" & ligne).Value = "OUI" Then Me.CheckBox3.Value = True 'à l'organisation du quotidien If Sheets("organisation au domicile").Range("F" & ligne).Value = "OUI" Then Me.CheckBox4.Value = True 'aux tâches domestiques If Sheets("organisation au domicile").Range("G" & ligne).Value = "OUI" Then Me.CheckBox5.Value = True 'à quelle fréquence ? If Sheets("organisation au domicile").Range("H" & ligne).Value = Me.Optionfreq1.Caption Then Me.Optionfreq1.Value = True If Sheets("organisation au domicile").Range("H" & ligne).Value = Me.Optionfreq2.Caption Then Me.Optionfreq2.Value = True If Sheets("organisation au domicile").Range("H" & ligne).Value = Me.Optionfreq3.Caption Then Me.Optionfreq3.Value = True 'quelles activités prenez-vous en charge ? 'travaux ménagers If Sheets("organisation au domicile").Range("I" & ligne).Value = "OUI" Then Me.CheckBox6.Value = True 'courses If Sheets("organisation au domicile").Range("J" & ligne).Value = "OUI" Then Me.CheckBox7.Value = True 'repas If Sheets("organisation au domicile").Range("K" & ligne).Value = "OUI" Then Me.CheckBox8.Value = True 'entretien du linge If Sheets("organisation au domicile").Range("L" & ligne).Value = "OUI" Then Me.CheckBox9.Value = True 'gestion du budget If Sheets("organisation au domicile").Range("M" & ligne).Value = "OUI" Then Me.CheckBox10.Value = True 'démarches administratives If Sheets("organisation au domicile").Range("N" & ligne).Value = "OUI" Then Me.CheckBox11.Value = True 'autres If Sheets("organisation au domicile").Range("O" & ligne).Value = "OUI" Then Me.CheckBox12.Value = True 'si autres, précisez : Me.TextBox1.Value = Sheets("organisation au domicile").Range("P" & ligne).Value 'certaines activités sont-elles inhabituelles pour vous ? If Sheets("organisation au domicile").Range("Q" & ligne).Value = Me.OptionACTIN1.Caption Then Me.OptionACTIN1.Value = True If Sheets("organisation au domicile").Range("Q" & ligne).Value = Me.OptionACTIN2.Caption Then Me.OptionACTIN2.Value = True 'si oui, lesquelles: Me.TextBox2.Value = Sheets("organisation au domicile").Range("R" & ligne).Value 'partagez-vous cette aide avec d'autres personnes de votre entourage ? If Sheets("organisation au domicile").Range("S" & ligne).Value = Me.OptionAIDAT1.Caption Then Me.OptionAIDAT1.Value = True If Sheets("organisation au domicile").Range("S" & ligne).Value = Me.OptionAIDAT2.Caption Then Me.OptionAIDAT2.Value = True 'si oui, à quelle fréquence ? If Sheets("organisation au domicile").Range("T" & ligne).Value = Me.OptionFREQAT1.Caption Then Me.OptionFREQAT1.Value = True If Sheets("organisation au domicile").Range("T" & ligne).Value = Me.OptionFREQAT2.Caption Then Me.OptionFREQAT2.Value = True If Sheets("organisation au domicile").Range("T" & ligne).Value = Me.OptionFREQAT3.Caption Then Me.OptionFREQAT3.Value = True 'si non, auriez-vous souhaité partager cette aide ? If Sheets("organisation au domicile").Range("U" & ligne).Value = Me.OptionPARTAID1.Caption Then Me.OptionPARTAID1.Value = True If Sheets("organisation au domicile").Range("U" & ligne).Value = Me.OptionPARTAID2.Caption Then Me.OptionPARTAID2.Value = True 'commentaires: Me.TextBox3.Value = Sheets("organisation au domicile").Range("V" & ligne).Value 'avez-vous envisagé un relais en cas d'empêchement de votre part ? If Sheets("organisation au domicile").Range("W" & ligne).Value = Me.OptionRELAIS1.Caption Then Me.OptionRELAIS1.Value = True If Sheets("organisation au domicile").Range("W" & ligne).Value = Me.OptionRELAIS2.Caption Then Me.OptionRELAIS2.Value = True 'si oui, à qui feriez-vous appel ? If Sheets("organisation au domicile").Range("X" & ligne).Value = Me.OptionAPPEL1.Caption Then Me.OptionAPPEL1.Value = True If Sheets("organisation au domicile").Range("X" & ligne).Value = Me.OptionAPPEL2.Caption Then Me.OptionAPPEL2.Value = True If Sheets("organisation au domicile").Range("X" & ligne).Value = Me.OptionAPPEL3.Caption Then Me.OptionAPPEL3.Value = True If Sheets("organisation au domicile").Range("X" & ligne).Value = Me.OptionAPPEL4.Caption Then Me.OptionAPPEL4.Value = True If Sheets("organisation au domicile").Range("X" & ligne).Value = Me.OptionAPPEL5.Caption Then Me.OptionAPPEL5.Value = True If Sheets("organisation au domicile").Range("X" & ligne).Value = Me.OptionAPPEL6.Caption Then Me.OptionAPPEL6.Value = True 'si 'autre', précisez: Me.TextBox4.Value = Sheets("organisation au domicile").Range("Y" & ligne).Value End With End If End Function
Merci d'avance pour toutes les réponses que je pourrais avoir! ;-) -
En relisant tes postes, je pense que ton approche est pas la bonne, si j'ai bien compris, quand tu clic sur suivant/Précédant c'est le dossier suivant/précédant que tu veux afficher et ce jusque.. 17 dossier suivant.
C'est bien ça ?
Et tu peu aussi remplacer...If Range("H" & ligne).Value = Me.Optionnb_0.Caption Then Me.Optionnb_0.Value = True If Range("H" & ligne).Value = Me.Optionnb_1.Caption Then Me.Optionnb_1.Value = True If Range("H" & ligne).Value = Me.Optionnb_2.Caption Then Me.Optionnb_2.Value = True If Range("H" & ligne).Value = Me.Optionnb_3.Caption Then Me.Optionnb_3.Value = True If Range("H" & ligne).Value = Me.Optionnb_4.Caption Then Me.Optionnb_4.Value = True If Range("H" & ligne).Value = Me.Optionnb_5.Caption Then Me.Optionnb_5.Value = True
par en plus simple...For i = 1 To 5 If Me.Controls("Optionnb_" & i).Caption = Range("H" & ligne).Value Then Me.Controls("Optionnb_" & i) = True: Exit For End If Next i
A+ -
Non en fait pour un dossier j'ai 17 UserForm qui me permettent de saisir l'ensemble des données qui sont elles stockées dans des feuilles Excel. Sur toutes ces feuilles Excel, une ligne correspond à un numéro de dossier!
Le problème que j'ai quand on veut revenir sur un dossier existant. Dans ce cas je veux que les userForm s'affichent avec les données saisies ultérieurement! D'où les fonctions 'recupDossier' (une pour chaque userForm).
Et donc elles fonctionnent bien pour tous les userForm, SAUF pour les TextBox et CheckBox des UF 8, 9, 11, 12, et 13 !-
Je pense qu'ont est pas sur les même rails.
Quand je dis les même je parle du design et pas des données .
Et, toujours si je comprend bien ce qui change c'est..
With Sheets("organisation au domicile")..
Tu pourrais mettre un exemple de ton classeur (sans données confidentielles, des nom fictifs) sur CiJoint.fr ou Cjoint.com
et mettre le lien sur un poste suivant.
-
-
non les userForms n'ont pas le même design! Chaque userForm permet de saisir des infos différentes sur un même numéro de dossier (= patient). Toutes ces infos sont stockées sur différentes feuilles Excel (patient, aidant, maladie patient, conditions de vie, relations familliales, organisation au domicile, vie sociale et économique, vie professionnelle, besoins et attentes, zarit, SF-12).
Il y a effectivement le nom des feuilles (With Sheets("organisation au domicile")..) qui change mais aussi : ce ne sont pas les mêmes infos que je vais chercher ni les mêmes 'contrôles' que je remplis(car différents userForm).
Je sais pas si j'ai été claire? J'ai essayé en tous cas :-p !! -
Oui, cette fois cela me parais plus limpide.
Mais pour trouver les erreurs ce ne sera pas possible sans ton classeur. -
ben je ne sais pas comment mettre de pièce jointe sur ce forum! je veux bien vous l'envoyer par mail si vous vous sentez motivé à y jeter un coup d'oeil?!?!
-
voici le lien du fichier:
http://www.cijoint.fr/cjlink.php?file=cj201103/cijUzeRBrJ.xls
je ne connaissais pas cijoint.fr! C'est impéccable! ;-)