Format de la cellule excel avec userform

Fermé
Mipou128 - 26 mars 2020 à 03:18
yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 - 27 mars 2020 à 08:39
Bonjour,

J'aimerais faire en sorte que mes cellules qui reçoivent l'information de mes textbox d'un userform soit formater dans les valeurs suivantes.

Textbox2 = date
Textbox 4 = monétaire
Textbox 5 = monétaire
Textbox 6 = %
Textbox 7 =%

Je dois être en mesure d'utiliser ces cellules pour faire une formule par la suite.

Voici mon code

Option Explicit
Dim Ws As Worksheet

Private Sub CommandButton6_Click()
Calendrier.Show
End Sub

'Pour le formulaire
Private Sub UserForm_Initialize()
Dim j As Long
Set Ws = Sheets("Factures") 'Correspond au nom de l'onglet dans le fichier Excel
With Me.ComboBox1
For j = 2 To Ws.Range("A" & Rows.Count).End(xlUp).Row
.AddItem Ws.Range("A" & j)
Next j
End With
Set Ws = Sheets("Liste") 'Correspond au nom de l'onglet dans le fichier Excel
With Me.ComboBox2
For j = 2 To Ws.Range("A" & Rows.Count).End(xlUp).Row
.AddItem Ws.Range("A" & j)
Next j
End With
If IsNumeric(TextBox3) Then [COLOR="SeaGreen"]
End Sub
'Pour la liste d?roulante Num?ro de facture
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
Private Sub ComboBox2_Change()
'Pour la liste d?roulante ?l?ments projet
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 Ajouter la facture
Private Sub CommandButton1_Click()
Dim L As Integer
Dim cellule As Range
If MsgBox("Confirmez-vous l'ajout de cette facture ?", vbYesNo, "Demande de confirmation d?ajout") = vbYes Then
L = Worksheets("Factures").Range("A" & Rows.Count).End(xlUp).Row + 1 'Pour placer le nouvel enregistrement ? la premi?re ligne de tableau non vide
Worksheets("Factures").Range("A" & L).Value = ComboBox1
Worksheets("Factures").Range("B" & L).Value = ComboBox2
Worksheets("Factures").Range("C" & L).Value = TextBox1
Worksheets("Factures").Range("D" & L).Value = TextBox2
Worksheets("Factures").Range("E" & L).Value = TextBox3
Worksheets("Factures").Range("F" & L).Value = TextBox4
Worksheets("Factures").Range("G" & L).Value = TextBox5
Worksheets("Factures").Range("H" & L).Value = TextBox6
Worksheets("Factures").Range("I" & L).Value = TextBox7


Dim j As Long
Dim i As Integer
ComboBox1.Clear
Set Ws = Sheets("Factures")
With Me.ComboBox1
For j = 2 To Ws.Range("A" & Rows.Count).End(xlUp).Row
.AddItem Ws.Range("A" & j)
Next j
End With
Else
'Rien
End If
ComboBox1.Value = ""
ComboBox2.Value = ""
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
End Sub
'Pour le bouton Modifier une facture
Private Sub CommandButton3_Click()

If ComboBox1.Text = "" Then
MsgBox ("Veuillez s?lectionner un num?ro de facture avec la liste d?roulante")

Exit Sub
End If
If MsgBox("Confirmez-vous la modification de cette facure ?", vbYesNo, "Demande de confirmation de modification") = vbYes Then
Dim ligne As Long
Dim i As Integer
If Me.ComboBox1.ListIndex = -1 Then Exit Sub
ligne = Me.ComboBox1.ListIndex + 2
Ws.Cells(ligne, "B") = ComboBox2
For i = 1 To 7
Ws.Cells(ligne, i + 2) = Me.Controls("TextBox" & i)
Next i
End If
End Sub
'Supprimer une ligne
Private Sub CommandButton4_Click()
Dim cell As Range
If MsgBox("Confirmez-vous la suppression de cette facture ?", vbYesNo, "Demande de confirmation de suppression") = vbYes Then
With Sheets("Factures")
Set cell = .Columns("A").Find(ComboBox1, LookAt:=xlWhole)
If Not cell Is Nothing Then cell.EntireRow.Delete
End With
MsgBox ("Facture supprim?e avec succ?s")
End If
ComboBox1.Value = ""
ComboBox2.Value = ""
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
End Sub
'Pour le bouton Quitter
Private Sub CommandButton5_Click()
Unload Me
End Sub



Configuration: Windows / Edge 18.17763
A voir également:

2 réponses

franc38 Messages postés 197 Date d'inscription mercredi 23 avril 2008 Statut Membre Dernière intervention 27 février 2023 38
Modifié le 26 mars 2020 à 07:29
Bonjour
le plus simple est de formater les cellules qui reçoivent les données...

sinon

Worksheets("Factures").Range("D" & L).Value = format(TextBox2,"dd/mm/yyyy")
Worksheets("Factures").Range("F" & L).Value = Format(TextBox4.Value, "#.## €")
Worksheets("Factures").Range("G" & L).Value = Format(TextBox5.Value, "#.## €")
Worksheets("Factures").Range("H" & L).Value = Format(TextBox6, "0.00%")
Worksheets("Factures").Range("I" & L).Value =Format(TextBox7, "0.00%")

A+ François
0
yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 1 471
26 mars 2020 à 09:24
François, bonjour, le code que tu proposes va probablement enregistrer du texte, et pas des nombres.
0
yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 1 471
26 mars 2020 à 09:22
bonjour, merci d'utiliser les balises de code, comme expliqué: https://codes-sources.commentcamarche.net/faq/11288-les-balises-de-code

le plus simple me semble de formater les colonnes à l'avance.

si tu veux les formater au fur et à mesure à partir du VBA:
Worksheets("Factures").Range("D" & L).NumberFormat= "dd/mm/yyyy"
0
J'insère cette formule ou ?
0
yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 1 471 > Mipou128
27 mars 2020 à 08:39
en dessous de
Worksheets("Factures").Range("D" & L).Value = TextBox2
0