XL 2019 Faire comuniquer un tabeau Excel avec un Userform

thunder23

XLDnaute Occasionnel
Bonsoir le forum,

Je souhaite que quand j'appelle un Userform celui-ci se rempli des valeurs. Je ne vois que par des Textbox sauf qu'il faut un certain type de boucle et je ne sais pas comment faire.

J'ai mis un fichier en exemple afin de mieux comprendre se que je recherche.

Merci d'avance pour vos réponses ;)
 

Pièces jointes

  • test1.xlsm
    41.6 KB · Affichages: 25
Solution
Bonsoir.
'aurais peut être plutôt mis une ListBox, mais tel que votre UFm est dessiné :
VB:
Option Explicit
Private RngDon As Range, TDon()
Private Sub UserForm_Initialize()
   Dim L&, C&
   Set RngDon = ActiveSheet.[A3:F14]
   TDon = RngDon.Value
   For L = 1 To UBound(TDon, 1)
      Me("Label" & L).Caption = TDon(L, 1)
      For C = 2 To 6
         Me("TextBox" & (L - 1) * 5 + C - 1).Text = TDon(L, C)
         Next C, L
   End Sub
Private Sub CommandButton1_Click()
   Dim L&, C&
   Set RngDon = ActiveSheet.[A3:F14]
   TDon = RngDon.Value
   For L = 1 To UBound(TDon, 1)
      For C = 2 To 6
         If C < 4 Then
            TDon(L, C) = CDate(Me("TextBox" & (L - 1) * 5 + C - 1).Text)
         Else
            TDon(L, C) =...

Dranreb

XLDnaute Barbatruc
Bonsoir.
'aurais peut être plutôt mis une ListBox, mais tel que votre UFm est dessiné :
VB:
Option Explicit
Private RngDon As Range, TDon()
Private Sub UserForm_Initialize()
   Dim L&, C&
   Set RngDon = ActiveSheet.[A3:F14]
   TDon = RngDon.Value
   For L = 1 To UBound(TDon, 1)
      Me("Label" & L).Caption = TDon(L, 1)
      For C = 2 To 6
         Me("TextBox" & (L - 1) * 5 + C - 1).Text = TDon(L, C)
         Next C, L
   End Sub
Private Sub CommandButton1_Click()
   Dim L&, C&
   Set RngDon = ActiveSheet.[A3:F14]
   TDon = RngDon.Value
   For L = 1 To UBound(TDon, 1)
      For C = 2 To 6
         If C < 4 Then
            TDon(L, C) = CDate(Me("TextBox" & (L - 1) * 5 + C - 1).Text)
         Else
            TDon(L, C) = CDbl(Me("TextBox" & (L - 1) * 5 + C - 1).Text)
            End If
         Next C, L
   RngDon.Value = TDon
   End Sub
 

thunder23

XLDnaute Occasionnel
Bonsoir.
'aurais peut être plutôt mis une ListBox, mais tel que votre UFm est dessiné :
VB:
Option Explicit
Private RngDon As Range, TDon()
Private Sub UserForm_Initialize()
   Dim L&, C&
   Set RngDon = ActiveSheet.[A3:F14]
   TDon = RngDon.Value
   For L = 1 To UBound(TDon, 1)
      Me("Label" & L).Caption = TDon(L, 1)
      For C = 2 To 6
         Me("TextBox" & (L - 1) * 5 + C - 1).Text = TDon(L, C)
         Next C, L
   End Sub
Private Sub CommandButton1_Click()
   Dim L&, C&
   Set RngDon = ActiveSheet.[A3:F14]
   TDon = RngDon.Value
   For L = 1 To UBound(TDon, 1)
      For C = 2 To 6
         If C < 4 Then
            TDon(L, C) = CDate(Me("TextBox" & (L - 1) * 5 + C - 1).Text)
         Else
            TDon(L, C) = CDbl(Me("TextBox" & (L - 1) * 5 + C - 1).Text)
            End If
         Next C, L
   RngDon.Value = TDon
   End Sub

Bonsoir Dranreb, Bonsoir le forum,

Le soucis est que les listbox je n'ai jamais essayer, est-ce que l'on peut lui mettre une mise en forme ?

Par ailleurs merci pour votre code, je vais le mettre sur mon fichier final ;)
 

thunder23

XLDnaute Occasionnel
Bonjour,

Je reviens sur ce sujet car je ne vois pas comment inclure une condition en plus. En fait j'ai un autre UserForm que je voudrais afficher un aperçu de mon planning sur l'année mais en tenant compte si c'est une année à 28 ou 29 pour le mois de Février. J'ai mis un fichier pour mieux comprendre la chose.

Merci d'avance ;)
 

Pièces jointes

  • testaperçu.xlsm
    30.3 KB · Affichages: 8

Dranreb

XLDnaute Barbatruc
Bonjour.
Je dirais comme ça :
VB:
Private Sub UserForm_Initialize()
   Dim L As Integer, TDon(), M As Integer, J As Integer, Lab As MSForms.Label, TBx As MSForms.TextBox, I As Integer
   TDon = Feuil1.[A2].Resize(366, 2).Value
   For L = 1 To DateSerial(Year(TDon(1, 1)) + 1, 1, 1) - TDon(1, 1)
      M = Month(TDon(L, 1)): J = Day(TDon(L, 1))
      Set Lab = Me("Label" & L)
      Set TBx = Me("TextBox" & L)
      Lab.Caption = J
      TBx.Text = TDon(L, 2)
      Lab.Left = 18 + (M - 1) * 84: Lab.Width = 12: Lab.Top = 25.5 + (J - 1) * 18: Lab.Height = 12
      TBx.Left = 33 + (M - 1) * 84: TBx.Width = 36: TBx.Top = 24 + (J - 1) * 18: TBx.Height = 15
      Select Case TDon(L, 2)
         Case "M": TBx.BackColor = RGB(242, 8, 132)
         Case "S": TBx.BackColor = RGB(0, 204, 255)
         Case "N": TBx.BackColor = RGB(31, 183, 20)
         Case "J": TBx.BackColor = RGB(255, 215, 45)
         Case "REM": TBx.BackColor = RGB(240, 255, 45)
         Case "CP": TBx.BackColor = RGB(255, 153, 0) 'ou 255.102.0
         Case "CPN": TBx.BackColor = RGB(255, 153, 0) 'ou 255.102.0
         Case "EF": TBx.BackColor = RGB(255, 153, 204)
         Case "EFN": TBx.BackColor = RGB(255, 153, 204)
         Case "AM": TBx.BackColor = RGB(255, 0, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "JCN": TBx.BackColor = RGB(153, 204, 0)
         Case "HAR": TBx.BackColor = RGB(204, 204, 255)
         Case "RSU": TBx.BackColor = RGB(255, 204, 153)
         Case "REC": TBx.BackColor = RGB(255, 255, 255)
         Case "GRV": TBx.BackColor = RGB(153, 102, 51)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "FOS": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "FOSN": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "DEL": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "DELN": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "ACTP": TBx.BackColor = RGB(0, 0, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "AAN": TBx.BackColor = RGB(166, 166, 166)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         End Select
      Next L
   Label366.Visible = L > 366: TextBox366.Visible = L > 366
End Sub
 

thunder23

XLDnaute Occasionnel
Bonjour.
Je dirais comme ça :
VB:
Private Sub UserForm_Initialize()
   Dim L As Integer, TDon(), M As Integer, J As Integer, Lab As MSForms.Label, TBx As MSForms.TextBox, I As Integer
   TDon = Feuil1.[A2].Resize(366, 2).Value
   For L = 1 To DateSerial(Year(TDon(1, 1)) + 1, 1, 1) - TDon(1, 1)
      M = Month(TDon(L, 1)): J = Day(TDon(L, 1))
      Set Lab = Me("Label" & L)
      Set TBx = Me("TextBox" & L)
      Lab.Caption = J
      TBx.Text = TDon(L, 2)
      Lab.Left = 18 + (M - 1) * 84: Lab.Width = 12: Lab.Top = 25.5 + (J - 1) * 18: Lab.Height = 12
      TBx.Left = 33 + (M - 1) * 84: TBx.Width = 36: TBx.Top = 24 + (J - 1) * 18: TBx.Height = 15
      Select Case TDon(L, 2)
         Case "M": TBx.BackColor = RGB(242, 8, 132)
         Case "S": TBx.BackColor = RGB(0, 204, 255)
         Case "N": TBx.BackColor = RGB(31, 183, 20)
         Case "J": TBx.BackColor = RGB(255, 215, 45)
         Case "REM": TBx.BackColor = RGB(240, 255, 45)
         Case "CP": TBx.BackColor = RGB(255, 153, 0) 'ou 255.102.0
         Case "CPN": TBx.BackColor = RGB(255, 153, 0) 'ou 255.102.0
         Case "EF": TBx.BackColor = RGB(255, 153, 204)
         Case "EFN": TBx.BackColor = RGB(255, 153, 204)
         Case "AM": TBx.BackColor = RGB(255, 0, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "JCN": TBx.BackColor = RGB(153, 204, 0)
         Case "HAR": TBx.BackColor = RGB(204, 204, 255)
         Case "RSU": TBx.BackColor = RGB(255, 204, 153)
         Case "REC": TBx.BackColor = RGB(255, 255, 255)
         Case "GRV": TBx.BackColor = RGB(153, 102, 51)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "FOS": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "FOSN": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "DEL": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "DELN": TBx.BackColor = RGB(164, 82, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "ACTP": TBx.BackColor = RGB(0, 0, 0)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         Case "AAN": TBx.BackColor = RGB(166, 166, 166)
            TBx.ForeColor = &HFFFFFF: TBx.Font.Bold = True
         End Select
      Next L
   Label366.Visible = L > 366: TextBox366.Visible = L > 366
End Sub
Excuse-moi de t'embêter encore une fois, j'essaye de modifier le format dans le label pour mettre "dim 01" par exemple et pouvoir également mettre une couleur si c'est un dimanche ou jours fériés.
Si tu as l'idée voici la plage de cellule oùsont noté les jours fériés sur mon fichier final (BDD27:BDD39) car j'essaye de percer ton code mais trop avancer pour moi pour le coup 😳
 

ChTi160

XLDnaute Barbatruc
Bonjour thunder23
Bonjour Bernard
Ce que j'ai mis pour colorer les Sam et Dim
VB:
Lab.Caption = Application.Proper(Format(TDon(L, 1), "ddd d")) 'j
Lab.BackColor = IIf(Weekday(TDon(L, 1), vbMonday) = 6, vbYellow, IIf(Weekday(TDon(L, 1), vbMonday) = 7, vbRed, vbWhite))
j'ai aussi une question pour Bernard si je peux me permettre
pourquoi déclares tu "TDon()" et non "TDon"
merci par avance
Jean marie
 

Dranreb

XLDnaute Barbatruc
pourquoi déclares tu "TDon()" et non "TDon"
Je ne vois pas l'intérêt de travailler avec un Variant contenant un tableau quand il est possible de travailler directement avec un tableau. Ce n'est pas toujours possible, quand par exemple ce sont plusieurs tableaux rangés dans une Collection ou un autre tableau, qu'on veut pouvoir parcourir avec un For Each, car seuls les objets et les Variant sont acceptés dans une telle boucle.
 
Dernière édition:

thunder23

XLDnaute Occasionnel
Bonjour le forum,

J'ai trouvé ce code sur un autre forum, ça à l'air d'être se que je souhaite mais je n'arrive pas à l'adapter à mon UserForm, ça me met error 424 ! 😭

VB:
Private Sub TestLBL()
Dim i%
  For i = 1 To 366 'boucle pour la création des objets textbox
    If frmCalendrier.Controls("Label" & i).Caption = "" Then frmCalendrier.Controls("Label" & i).BackColor = &H80000005: GoTo suite
      If WorksheetFunction.CountIf(Feuil3.Range("AJ27:AJ39"), DateSerial(Feuil3.Range("AG12").Value, Feuil3.Range("AI4").Value, frmCalendrier.Controls("Label" & i).Caption * 1)) > 0 Then  'Si le text de ce label est fériés alors
          frmCalendrier.Controls("Label" & i).BackColor = RGB(255, 0, 0)  'On colorie le fond du label en rouge
      Else 'sinon
          frmCalendrier.Controls("Label" & i).BackColor = &H80000005 ' on colorie le texte du label en blanc
      End If
suite:
  Next i
End Sub
 

Discussions similaires

  • Résolu(e)
Microsoft 365 Taille UserForm
Réponses
3
Affichages
327

Statistiques des forums

Discussions
312 096
Messages
2 085 254
Membres
102 839
dernier inscrit
Tougtoug