Format de la cellule excel avec userform
Mipou128
-
yg_be Messages postés 23541 Date d'inscription Statut Contributeur Dernière intervention -
yg_be Messages postés 23541 Date d'inscription Statut Contributeur Dernière intervention -
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
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:
- Format de la cellule excel avec userform
- Format epub - Guide
- Format factory - Télécharger - Conversion & Codecs
- Excel cellule couleur si condition texte - Guide
- Liste déroulante excel - Guide
- Proteger cellule excel - Guide
2 réponses
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
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
yg_be
Messages postés
23541
Date d'inscription
Statut
Contributeur
Dernière intervention
1 584
François, bonjour, le code que tu proposes va probablement enregistrer du texte, et pas des nombres.
yg_be
Messages postés
23541
Date d'inscription
Statut
Contributeur
Dernière intervention
Ambassadeur
1 584
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:
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"