VBA - Renseigner plusieurs textbox avec module de classe

piga25

XLDnaute Barbatruc
Bonjour,
Malgré mes recherches, je ne suis pas arrivé à mes fins. Néanmoins je pense qu'il faut passer par un module de classe.

Voila ce que j'aimerais faire dans l'Userform7 (voir image)
- Renseigner la textBox1 (date) que si la textBox2 (heure) est renseignée avec la date du jour (par défaut) et avec la possibilité d'en choisir une avec le calendrier (Userform5) - Code d'appel dans module 5: Sub Fait()
Au total il y a 18 textbox
Les textBox sont réparties dans les pages : Avant le tir - Après le tir - Retour sur zone


1026603


En vous remerciant
 

Pièces jointes

  • Main courante 2019 version 5.022.xlsm
    395.9 KB · Affichages: 8
Dernière édition:

piga25

XLDnaute Barbatruc
Bonjour,

ma demande serait pour remplacer ceci :
Cela fonctionne mais je n'aime pas du tout car je sais que l'on peut réduire cela à seulement quelques lignes.

code dans Userform7
VB:
Private Sub TextBox2_Change()
TextBox1.Value = Format(Now, "dd/mm/yyyy")
If TextBox2 = "" Then
TextBox1.Value = ""
End If
End Sub
Private Sub TextBox1_Enter()
txb = 1
fait
End Sub

Private Sub TextBox8_Change()
TextBox7.Value = Format(Now, "dd/mm/yyyy")
If TextBox8 = "" Then
TextBox7.Value = ""
End If
End Sub
Private Sub TextBox7_Enter()
txb = 1
fait
End Sub

Private Sub TextBox14_Change()
TextBox13.Value = Format(Now, "dd/mm/yyyy")
If TextBox14 = "" Then
TextBox13.Value = ""
End If
End Sub
Private Sub TextBox13_Enter()
txb = 13
fait
End Sub

Private Sub TextBox20_Change()
TextBox19.Value = Format(Now, "dd/mm/yyyy")
If TextBox20 = "" Then
TextBox19.Value = ""
End If
End Sub
Private Sub TextBox19_Enter()
txb = 19
fait
End Sub

Private Sub TextBox26_Change()
TextBox25.Value = Format(Now, "dd/mm/yyyy")
If TextBox26 = "" Then
TextBox25.Value = ""
End If
End Sub
Private Sub TextBox25_Enter()
txb = 25
fait
End Sub

Private Sub TextBox32_Change()
TextBox31.Value = Format(Now, "dd/mm/yyyy")
If TextBox32 = "" Then
TextBox31.Value = ""
End If
End Sub
Private Sub TextBox31_Enter()
txb = 31
fait
End Sub

Private Sub TextBox38_Change()
TextBox37.Value = Format(Now, "dd/mm/yyyy")
If TextBox38 = "" Then
TextBox37.Value = ""
End If
End Sub
Private Sub TextBox37_Enter()
txb = 37
fait
End Sub

Private Sub TextBox44_Change()
TextBox43.Value = Format(Now, "dd/mm/yyyy")
If TextBox44 = "" Then
TextBox43.Value = ""
End If
End Sub
Private Sub TextBox43_Enter()
txb = 43
fait
End Sub

Private Sub TextBox50_Change()
TextBox49.Value = Format(Now, "dd/mm/yyyy")
If TextBox50 = "" Then
TextBox49.Value = ""
End If
End Sub
Private Sub TextBox49_Enter()
txb = 49
fait
End Sub

Private Sub TextBox56_Change()
TextBox55.Value = Format(Now, "dd/mm/yyyy")
If TextBox56 = "" Then
TextBox55.Value = ""
End If
End Sub
Private Sub TextBox55_Enter()
txb = 55
fait
End Sub

Private Sub TextBox62_Change()
TextBox61.Value = Format(Now, "dd/mm/yyyy")
If TextBox62 = "" Then
TextBox61.Value = ""
End If
End Sub
Private Sub TextBox61_Enter()
txb = 61
fait
End Sub

Private Sub TextBox68_Change()
TextBox67.Value = Format(Now, "dd/mm/yyyy")
If TextBox68 = "" Then
TextBox67.Value = ""
End If
End Sub
Private Sub TextBox67_Enter()
txb = 67
fait
End Sub

Private Sub TextBox74_Change()
TextBox73.Value = Format(Now, "dd/mm/yyyy")
If TextBox74 = "" Then
TextBox73.Value = ""
End If
End Sub
Private Sub TextBox73_Enter()
txb = 73
fait
End Sub

Private Sub TextBox80_Change()
TextBox79.Value = Format(Now, "dd/mm/yyyy")
If TextBox80 = "" Then
TextBox79.Value = ""
End If
End Sub
Private Sub TextBox79_Enter()
txb = 79
fait
End Sub

Private Sub TextBox86_Change()
TextBox85.Value = Format(Now, "dd/mm/yyyy")
If TextBox86 = "" Then
TextBox85.Value = ""
End If
End Sub
Private Sub TextBox85_Enter()
txb = 85
fait
End Sub

Private Sub TextBox92_Change()
TextBox91.Value = Format(Now, "dd/mm/yyyy")
If TextBox92 = "" Then
TextBox91.Value = ""
End If
End Sub
Private Sub TextBox91_Enter()
txb = 91
fait
End Sub

Private Sub TextBox98_Change()
TextBox97.Value = Format(Now, "dd/mm/yyyy")
If TextBox98 = "" Then
TextBox97.Value = ""
End If
End Sub
Private Sub TextBox97_Enter()
txb = 97
fait
End Sub

Private Sub TextBox104_Change()
TextBox103.Value = Format(Now, "dd/mm/yyyy")
If TextBox104 = "" Then
TextBox103.Value = ""
End If
End Sub
Private Sub TextBox103_Enter()
txb = 103
fait
End Sub

code dans module de classe : classe1
Code:
Public WithEvents bruno As MSForms.Label
Public WithEvents bru As MSForms.Label

Private Sub bru_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If X > 8 Or Y > 8 Then bru.BackColor = &H0&: Exit Sub
fait 'va a macro "fait"
bru.BackColor = &HFF0000
mmaa = CDate("01 " & UserForm5.Label50)
If bru.Caption < 15 And Right(bru.Name, Len(bru.Name) - 5) > 20 Then
mmaa = mmaa + 33
End If
If bru.Caption > 20 And Right(bru.Name, Len(bru.Name) - 5) < 20 Then
mmaa = mmaa - 20
End If
If txb = 2 Then UserForm4.TextBox2 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 17 Then UserForm2.TextBox17 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 1 Then UserForm7.TextBox1 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 7 Then UserForm7.TextBox7 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 13 Then UserForm7.TextBox13 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 19 Then UserForm7.TextBox19 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 25 Then UserForm7.TextBox25 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 31 Then UserForm7.TextBox31 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 37 Then UserForm7.TextBox37 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 43 Then UserForm7.TextBox43 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 49 Then UserForm7.TextBox49 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 55 Then UserForm7.TextBox55 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 61 Then UserForm7.TextBox61 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 67 Then UserForm7.TextBox67 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 73 Then UserForm7.TextBox73 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 79 Then UserForm7.TextBox79 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 85 Then UserForm7.TextBox85 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 91 Then UserForm7.TextBox91 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 97 Then UserForm7.TextBox97 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 103 Then UserForm7.TextBox103 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 4 Then UserForm6.TextBox4 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
If txb = 18 Then UserForm6.TextBox18 = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))

UserForm5.Hide
End Sub
 

piga25

XLDnaute Barbatruc
Bonjour,
A partir d'un code de JOB75 ( https://www.excel-downloads.com/thr...lic-sur-bouton-option.20029531/#post-20220627) J'ai de l'adapté à mon besoin.
Là, cela fonctionne avec les boutons optionbutton comme dans son exemple.

Par contre comment faire pour les 18 textbox (affichage date du jour si heure de renseignée et affichage calendrier si besoin changer date)
J'ai essayé de changer :
"OptionButton" par "TextBox"
OB.Caption par OB.Value
n = n + 1 par n = (n * 6) + 1
juste pour voir si j'avais bien le msgbox qui s'affiche, tout cela sans résultat.

Dans un module de classe (classe2), j'y ai mis ce code :
VB:
Public WithEvents OB As MSForms.OptionButton

Private Sub OB_Click()
MsgBox "OptionButton n° " & OB.Caption
End Sub

La classe est initialisée dans Module8 :
VB:
Public OB() As New Classe2

Sub USF()
Dim c As Control, n
With UserForm7
    For Each c In .Controls
        If TypeName(c) = "OptionButton" Then
            ReDim Preserve OB(n)
            Set OB(n).OB = c
            n = n + 1
        End If
    Next
   '.Show - J'ai mis cette ligne en annotation car cela me faisait cliquer deux fois sur le bouton quitter.
End With
End Sub

Puis dans le code initialise de Userform7
VB:
Private Sub UserForm_Initialize()
MultiPage1.Value = 0
T1 = ""
T1 = Application.Max(Feuil6.[A2:A65000]) + 1
T7 = "TIR n°" & T1.Value
USF
End Sub

Le bouton pour ouvrir Userform7 est situé dans l'Userform2 Image10
VB:
Private Sub Image10_Click()
Dim T1, T7 As Long
Unload UserForm2
UserForm7.Show
Me.MultiPage1.Value = 0
End Sub
 

ChTi160

XLDnaute Barbatruc
Bonsoir piga25
Bonsoir le Fil,le Forum
Question :
Que doivent être les Heures , format , mode de saisie , peut on imaginer avoir des Combobox à la place des TextBox(Heure) avec des Horaires pour la sélection?
Merci
jean marie
 

piga25

XLDnaute Barbatruc
Bonsoir ChTi160
Bonsoir le forum
Question :
Que doivent être les Heures , format , mode de saisie , peut on imaginer avoir des Combobox à la place des TextBox(Heure) avec des Horaires pour la sélection?
Les heures devront être entrée en format hh:mn (ex: 02:35 ou 22:20) et manuellement.
Si je mets des combobox, la liste sera longue car il me faut les heures toutes les 5 mn

Merci
 

ChTi160

XLDnaute Barbatruc
Re
Merci
donc Pas de plage Horaire Ex entre 6:00 et 22:00 , c'est 24 sur 24 ?

Une première approche
j'ai renommé les TextBox Date et Heure.
Puis on Double Click sur ces TextBox Date et Heure.
il n'y a pas les Module permettant d'initialiser le Userform Nommé UsfCalendrier
mais il s'affiche voir pour le Placer lors de l'affichage .
Amicalement
jean marie
 

Pièces jointes

  • FBD60000-piga25_Chti160.xlsm
    365.9 KB · Affichages: 31

piga25

XLDnaute Barbatruc
Bonjour ChTi160, le forum
Désolé pour le retard dans la réponse.

Voila pour ce que je pense avoir saisie:
Dans l'Userform7 tu boucles sur les textBox date et du mets dans un tableau que tu as défini "Option Base 1"

Par contre là je ne vois pas a quoi sert ceci : Sp_Change()

Dans le module de classe tu compares les noms des textbox date pour voir s'ils correspondent à la chaîne de caractères que tu as défini

Par contre je n'arrive pas a récupérer le nom de la textbox active ("TxtB_Date_*T#_#") pour y inserer la date. Cela serait une ligne dans classe 1
non de la textbox = CDate(bru.Caption & " " & Month(mmaa) & " " & Year(mmaa))
 

Discussions similaires

Réponses
9
Affichages
973

Statistiques des forums

Discussions
311 733
Messages
2 082 015
Membres
101 867
dernier inscrit
XFPRO