Problème macro insertion ligne User Form

juju12

XLDnaute Nouveau
Bonjour!!

Nous avons un gros problème :(
Nous avons creer un userform, lorsque l'on valide cela nous insère une ligne au dessu d'un tableau (ce que l'on veut) mais cela ne reprend absolument pas la mise en forme du tableau...et ça malheureusement ca ne nous va pas....mais on ne sais pas comment faire pour corriger cela.... svp aidez nous :))))


Private Sub Bt_Valider_Click()

Dim nr As Integer
If Me.UF_Listcli = "" Or Me.UF_Chantier = "" Or Me.UF_Nom = "" Or Me.UF_Adresse = "" Or Me.UF_Cp = "" Or Me.UF_Ville = "" Or Me.UF_Typecli = "" Or Me.UF_Com = "" Or Me.UF_ListDAS = "" Or Me.UF_Listtrav = "" Or Me.UF_MontTTC = "" Or Me.UF_ListtxTVA = "" Or Me.UF_MontTVA = "" Or Me.UF_MontHT = "" Or Me.UF_Date = "" Or Me.UF_Mois = "" Then

MsgBox "Saisir les champs incomplets"

GoTo Ending
Else

nr = 3
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 1).EntireRow.Insert 3

ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 1) = UF_Chantier
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 2) = UF_Nom
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 3) = UF_Prénom
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 4) = UF_Adresse
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 5) = UF_Cp.Value
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 6) = UF_Ville
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 7) = UF_Typecli
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 8) = UF_Com
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 9) = UF_ListDAS
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 10) = UF_Listtrav
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 11) = Round(UF_MontTTC.Value, 2)
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 12) = (CDbl(UF_ListtxTVA.Value))
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 13) = Round(UF_MontTVA.Value, 2)
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 14) = Round(UF_MontHT.Value, 2)
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 15) = UF_Date.Value
ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 16) = UF_Mois

Me.UF_Listcli = ""
Me.UF_Chantier = ""
Me.UF_Nom = ""
Me.UF_Prénom = ""
Me.UF_Adresse = ""
Me.UF_Cp = ""
Me.UF_Ville = ""
Me.UF_Typecli = ""
Me.UF_Com = ""
Me.UF_ListDAS = ""
Me.UF_Listtrav = ""
Me.UF_MontTTC = ""
Me.UF_ListtxTVA = ""
Me.UF_MontTVA = ""
Me.UF_MontHT = ""
Me.UF_Date = ""
Me.UF_Mois = ""


Me.UF_Listcli.SetFocus
Unload Me
End If
Ending:
End Sub
 

Robert

XLDnaute Barbatruc
Repose en paix
Re : Problème macro insertion ligne User Form

Bonjour Juju, bonjour le forum,

peut-être comme ça :
Code:
Private Sub Bt_Valider_Click()
Dim O As Worksheet
Dim DL As Long
Dim nr As Byte

Set O = Sheets("Nouveaux Chantiers")
DL = O.Cells(Application.Rows.Count, 1).End(xlUp).Row
If Me.UF_Listcli = "" Or Me.UF_Chantier = "" Or Me.UF_Nom = "" Or Me.UF_Adresse = "" Or Me.UF_Cp = "" Or Me.UF_Ville = "" Or Me.UF_Typecli = "" Or Me.UF_Com = "" Or Me.UF_ListDAS = "" Or Me.UF_Listtrav = "" Or Me.UF_MontTTC = "" Or Me.UF_ListtxTVA = "" Or Me.UF_MontTVA = "" Or Me.UF_MontHT = "" Or Me.UF_Date = "" Or Me.UF_Mois = "" Then
    MsgBox "Saisir les champs incomplets"
    Exit Sub
Else
    nr = 3
    O.Rows(DL).Copy O.Cells(nr, 1)
    O.Rows(nr).ClearContents
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 1) = UF_Chantier
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 2) = UF_Nom
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 3) = UF_Prénom
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 4) = UF_Adresse
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 5) = UF_Cp.Value
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 6) = UF_Ville
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 7) = UF_Typecli
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 8) = UF_Com
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 9) = UF_ListDAS
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 10) = UF_Listtrav
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 11) = Round(UF_MontTTC.Value, 2)
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 12) = (CDbl(UF_ListtxTVA.Value))
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 13) = Round(UF_MontTVA.Value, 2)
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 14) = Round(UF_MontHT.Value, 2)
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 15) = UF_Date.Value
    ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, 16) = UF_Mois
    
    Me.UF_Listcli = ""
    Me.UF_Chantier = ""
    Me.UF_Nom = ""
    Me.UF_Prénom = ""
    Me.UF_Adresse = ""
    Me.UF_Cp = ""
    Me.UF_Ville = ""
    Me.UF_Typecli = ""
    Me.UF_Com = ""
    Me.UF_ListDAS = ""
    Me.UF_Listtrav = ""
    Me.UF_MontTTC = ""
    Me.UF_ListtxTVA = ""
    Me.UF_MontTVA = ""
    Me.UF_MontHT = ""
    Me.UF_Date = ""
    Me.UF_Mois = ""
    
    Me.UF_Listcli.SetFocus
    Unload Me
End If
End Sub
 

Lone-wolf

XLDnaute Barbatruc
Re : Problème macro insertion ligne User Form

Bonjour juju

Je suppose que le tableau comporte les entêtes. Si c'est le cas, crée un nouveau formulaire et fait comme ceci.

Laisse le nom des TextBox par défaut ensuite, pour chaqu'une d'elles dans les propriétés:

TextBox1 Tag = 1 - TextBox2 Tag = 2 - TextBox3 Tag = 3 et ainsi de suite, veille à ne pas te tromper. Les Tags correspondes aux numéros des colonnes: 1 = A - 2 = B - 3 = C etc.

Une fois que tu as tout terminé adapte le code à ton fichier. Et pour avoir un rappel, Met en commentaire à quoi correpond chaque TextBox.

Exemple: 'Textbox1 = Nom - Textbox2 = Prenom - Textbox3 = Profession etc.


EDIT: Bonjour Robert :)

Code:
Private Sub CommandButton1_Click() 'Bouton Enregistrements
Dim col, derlig As Integer, i As Long
Dim ctrl As Control

With Sheets("Base")
derlig = .Range("a65536").End(xlUp).Row + 1

For Each ctrl In Me.Controls
col = Val(ctrl.Tag)
If col > 0 Then
If Not IsNumeric(ctrl) Then
.Cells(derlig, col) = ctrl
Else
.Cells(derlig, col) = CDbl(ctrl)   'format numérique simple ex: 3124
End If
End If
Next ctrl
.Range("G2:H65000").NumberFormat = "000 000 00 00"   'Numéro téléphone et fax
.Range("A:J").Columns.AutoFit
.Range("A2:J65000").Sort [A2], xlAscending
End With

For i = 1 To 10
Me.Controls("TextBox" & i).Text = ""
Next
TextBox10.SetFocus
End Sub
 
Dernière édition:

Dranreb

XLDnaute Barbatruc
Re : Problème macro insertion ligne User Form

Bonsoir.
Alors contrairement à votre dernière réponse dans cette discussion ça ne marche pas ?
Vous parlez de tableau. Mais cela signifie-t-il qu'il s'agit effectivement d'une plage mise sous forme de tableau ?
Sur mon Excel 2016 actuel on ne peut pas insérer de ligne ailleurs qu'à la fin d'un tel tableau par les moyens classiques.
Pour l'insérer en ligne 1 il faut faire pour cela LO.ListRows.Add(1)
LO étant une variable globale déclarée As ListObject et initialisée dans la UserForm_Initialize par Set LO = ThisWorkbook.Sheets("Nouveaux Chantiers").ListObjects(1) par exemple.
Vous pouvez d'ailleurs en profiter pour récupérer la plage représentant cette ligne au passage par :
VB:
Set PlgLgn = LO.ListRows.Add(1).Range
Ce sera quand même au moins déjà un peu mieux d'écrire 16 fois PlgLgn.Columns(x) plutôt que 16 fois ThisWorkbook.Sheets("Nouveaux Chantiers").Cells(nr, x) !
 
Dernière édition:

Lone-wolf

XLDnaute Barbatruc
Re : Problème macro insertion ligne User Form

Bonjour à tous :)

@juju: si le tableau est en couleur (vu que tu parle de la mise en forme), il suffit dans ce cas d'écrire

Code:
For x = 1 to 16
col = col + 1
With ThisWorkbook.Sheets("Nouveaux Chantiers")
.Cells(nr, col).Interior.Color = .Cells(nr + 1, col).Interior.Color
.Cells(nr, col).Font.Color = .Cells(nr + 1, col).Font.Color
End With
Next x
 

Dranreb

XLDnaute Barbatruc
Re : Problème macro insertion ligne User Form

(j'espère que le demandeur ne va pas ouvrir une 3ième discussion, toujours sans joindre de classeur modèle qu'on puisse lui retourner corrigé, parce là, il passerait direct dans ma liste d'ignorés !)
 

Statistiques des forums

Discussions
312 201
Messages
2 086 171
Membres
103 151
dernier inscrit
nassim