Code macro fonctionnant sous Excel 2007 mais pas 2013
bonjour,
j'ai écrit un code pour renseigner deux tableaux contenant des formules,, ce code fonctionne bien sous excel 2007 mais sous 2013, après deux lignes, il se plante avec le message d'erreur: la méthode "Add" de l'ogjet "LIstrows a échoué,, ci joint le code:
Option Explicit
' Nom de l'application
Const strAppName = "Saisie des clients"
Dim bNouveau As Boolean
Dim i As Integer
Dim j As Integer
Dim rng As Range
Dim cell As Range
Dim ligne As Range
Dim message As String
Dim LignTablo
Dim LignTablob
Dim TPaie As String
Dim Montant(8) As Double
Dim numero As String, Crédit As String
Dim aretire(8) As Double
Dim nouveau(8) As Double
Dim total(8) As Double
Private Sub CommandButton2_Click()
Unload UsfBdd
End Sub
Private Sub cmdNouveau_Click()
Init_Clients
bNouveau = True
End Sub
Private Sub Init_Clients()
With UsfBdd
TextNom = ""
TextAdress = ""
TextTel1 = ""
TextTel2 = ""
For i = 1 To 8
UsfBdd.Controls("ComboProduit" & i) = ""
UsfBdd.Controls("TextBox" & i) = ""
Next i
End With
End Sub
Private Sub lstclients_Click()
bNouveau = False
i = lstclients.ListIndex + 7
With ThisWorkbook.Worksheets("Clients")
UsfBdd.TextNom = .Cells(i, 1)
UsfBdd.TextAdress = .Cells(i, 2)
UsfBdd.TextTel1 = .Cells(i, 3)
UsfBdd.TextTel2 = .Cells(i, 4)
UsfBdd.ComboProduit1 = .Cells(i, 5)
UsfBdd.ComboProduit2 = .Cells(i, 9)
UsfBdd.ComboProduit3 = .Cells(i, 13)
UsfBdd.ComboProduit4 = .Cells(i, 17)
UsfBdd.ComboProduit5 = .Cells(i, 21)
UsfBdd.ComboProduit6 = .Cells(i, 25)
UsfBdd.ComboProduit7 = .Cells(i, 29)
UsfBdd.ComboProduit8 = .Cells(i, 33)
UsfBdd.TextBox1 = .Cells(i, 8)
UsfBdd.TextBox2 = .Cells(i, 12)
UsfBdd.TextBox3 = .Cells(i, 16)
UsfBdd.TextBox4 = .Cells(i, 20)
UsfBdd.TextBox5 = .Cells(i, 24)
UsfBdd.TextBox6 = .Cells(i, 28)
UsfBdd.TextBox7 = .Cells(i, 32)
UsfBdd.TextBox8 = .Cells(i, 36)
End With
End Sub
Private Sub UsfBdd_Initialize()
bNouveau = True
End Sub
Private Sub TextQtté_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr(1, ",0123456789", Chr(KeyAscii)) = 0 Then
KeyAscii = 0
MsgBox ("Les caractères autres que numériques ne sont pas autorisés")
End If
End Sub
Private Sub CommandButton1_Click()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
For i = 1 To 8
If UsfBdd.Controls("TextBox" & i) = "" Then
aretire(i) = 0
Else
aretire(i) = CDbl(UsfBdd.Controls("TextBox" & i))
End If
Next i
For i = 1 To 8
If UsfBdd.Controls("ListUnit" & i) = "t" Then
nouveau(i) = CDbl(UsfBdd.Controls("TextQtté" & i)) * Application.WorksheetFunction.Index(Range("densité"), Application.WorksheetFunction.Match(UsfBdd.Controls("ComboProduit" & i), Range("lesproduits"), 0))
Else
nouveau(i) = CDbl(UsfBdd.Controls("TextQtté" & i))
End If
Next i
For i = 1 To 8
total(i) = aretire(i) + nouveau(i)
Next i
For i = 1 To 8
If UsfBdd.Controls("ListUnit" & i) = "m3" Then
Montant(i) = Application.WorksheetFunction.Index(Range("metrecb"), Application.WorksheetFunction.Match(UsfBdd.Controls("ComboProduit" & i), Range("lesproduits"), 0))
End If
If UsfBdd.Controls("ListUnit" & i) = "t" Then
Montant(i) = Application.WorksheetFunction.Index(Range("Tonne"), Application.WorksheetFunction.Match(UsfBdd.Controls("ComboProduit" & i), Range("lesproduits"), 0))
End If
Next i
If TextNom = "" Then
MsgBox (message & "Vous devez saisir le nom de l'entreprise ou du client." & vbLf)
Exit Sub
End If
If ListTpaiement = "Comptant: Chèque" Then
LabelCheque.Visible = True
chèque.Visible = True
TPaie = "par Chèque n°" & chèque.Value
End If
If ListTpaiement = "Comptant: Espèce" Then
TPaie = ListTpaiement.Value
End If
If ListTpaiement = "Crédit" Then
ComboCrédit.Visible = True
Crédit = ComboCrédit
TPaie = ListTpaiement.Value
Else
Crédit = "Pas de crédit"
End If
With Sheets("bddclients").ListObjects("Tableau5510")
If .ListRows.Count = 0 Then
Range("Tableau5510[[#Headers],[ENTREPRISE]]").Offset(1, 0) = 1
Range("Tableau5510[[#Headers],[ENTREPRISE]]").Offset(1, 0) = ""
End If
If .ListRows.Count = 1 And .ListRows(1).Range.Cells(1, 1) = "" Then
Set LignTablo = Sheets("bddclients").ListObjects("Tableau5510").ListRows(1)
Else
Set LignTablo = Range("Tableau5510").ListObject.ListRows.Add(AlwaysInsert:=True)
End If
End With
With LignTablo.Range
LignTablo.Range.Cells(1, 1) = TextNom
LignTablo.Range.Cells(1, 2) = TextAdress
LignTablo.Range.Cells(1, 3) = TextTel1
LignTablo.Range.Cells(1, 4) = TextTel2
LignTablo.Range.Cells(1, 5) = ComboProduit1
LignTablo.Range.Cells(1, 6) = TextQtté1
LignTablo.Range.Cells(1, 7) = ListUnit1
LignTablo.Range.Cells(1, 8) = Montant(1)
LignTablo.Range.Cells(1, 9) = ComboProduit2
LignTablo.Range.Cells(1, 10) = TextQtté2
LignTablo.Range.Cells(1, 11) = ListUnit2
LignTablo.Range.Cells(1, 12) = Montant(2)
LignTablo.Range.Cells(1, 13) = ComboProduit3
LignTablo.Range.Cells(1, 14) = TextQtté3
LignTablo.Range.Cells(1, 15) = ListUnit3
LignTablo.Range.Cells(1, 16) = Montant(3)
LignTablo.Range.Cells(1, 17) = ComboProduit4
LignTablo.Range.Cells(1, 18) = TextQtté4
LignTablo.Range.Cells(1, 19) = ListUnit4
LignTablo.Range.Cells(1, 20) = Montant(4)
LignTablo.Range.Cells(1, 21) = ComboProduit5
LignTablo.Range.Cells(1, 22) = TextQtté5
LignTablo.Range.Cells(1, 23) = ListUnit5
LignTablo.Range.Cells(1, 24) = Montant(5)
LignTablo.Range.Cells(1, 25) = ComboProduit6
LignTablo.Range.Cells(1, 26) = TextQtté6
LignTablo.Range.Cells(1, 27) = ListUnit6
LignTablo.Range.Cells(1, 28) = Montant(6)
LignTablo.Range.Cells(1, 29) = ComboProduit7
LignTablo.Range.Cells(1, 30) = TextQtté7
LignTablo.Range.Cells(1, 31) = ListUnit7
LignTablo.Range.Cells(1, 32) = Montant(7)
LignTablo.Range.Cells(1, 33) = ComboProduit8
LignTablo.Range.Cells(1, 34) = TextQtté8
LignTablo.Range.Cells(1, 35) = ListUnit8
LignTablo.Range.Cells(1, 36) = Montant(8)
LignTablo.Range.Cells(1, 37) = CDate(VBA.Date)
LignTablo.Range.Cells(1, 38) = TPaie
LignTablo.Range.Cells(1, 39) = Application.CountA(Columns(1)) & "/" & "TGC-SA"
LignTablo.Range.Cells(1, 46) = Crédit
End With
With Worksheets("Clients")
If bNouveau = True Then
With Sheets("Clients").ListObjects("tclients")
If .ListRows.Count = 0 Then
Range("tclients[[#Headers],[CLIENTS]]").Offset(1, 0) = 1
Range("tclients[[#Headers],[CLIENTS]]").Offset(1, 0) = ""
End If
If .ListRows.Count = 1 And .ListRows(1).Range.Cells(1, 1) = "" Then
Set LignTablob = Sheets("Clients").ListObjects("tclients").ListRows(1)
Else
Set LignTablob = Range("tclients").ListObject.ListRows.Add(AlwaysInsert:=True)
End If
End With
With LignTablob.Range
LignTablob.Range.Cells(1, 1) = TextNom
LignTablob.Range.Cells(1, 2) = TextAdress
LignTablob.Range.Cells(1, 3) = TextTel1
LignTablob.Range.Cells(1, 4) = TextTel2
LignTablob.Range.Cells(1, 5) = ComboProduit1
LignTablob.Range.Cells(1, 6) = nouveau(1)
LignTablob.Range.Cells(1, 9) = ComboProduit2
LignTablob.Range.Cells(1, 10) = nouveau(2)
LignTablob.Range.Cells(1, 13) = ComboProduit3
LignTablob.Range.Cells(1, 14) = nouveau(3)
LignTablob.Range.Cells(1, 17) = ComboProduit4
LignTablob.Range.Cells(1, 18) = nouveau(4)
LignTablob.Range.Cells(1, 21) = ComboProduit5
LignTablob.Range.Cells(1, 22) = nouveau(5)
LignTablob.Range.Cells(1, 25) = ComboProduit6
LignTablob.Range.Cells(1, 26) = nouveau(6)
LignTablob.Range.Cells(1, 29) = ComboProduit7
LignTablob.Range.Cells(1, 30) = nouveau(7)
LignTablob.Range.Cells(1, 33) = ComboProduit8
LignTablob.Range.Cells(1, 34) = nouveau(8)
End With
Else
i = lstclients.ListIndex + 7
Sheets("Clients").Cells(i, 2) = UsfBdd.TextAdress
Sheets("Clients").Cells(i, 3) = UsfBdd.TextTel1
Sheets("Clients").Cells(i, 6) = total(1)
Sheets("Clients").Cells(i, 10) = total(2)
Sheets("Clients").Cells(i, 14) = total(3)
Sheets("Clients").Cells(i, 18) = total(4)
Sheets("Clients").Cells(i, 22) = total(5)
Sheets("Clients").Cells(i, 26) = total(6)
Sheets("Clients").Cells(i, 30) = total(7)
Sheets("Clients").Cells(i, 34) = total(8)
End If
End With
Unload UsfBdd
Sheets("bddclients").Activate
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Merci de m'aider à comprendre,, est ce mon code qui est mal conçu?
Bonsoir
Utilise plutôt cette syntaxe :
Set LignTablo = Range("Tableau5510").ListObject.ListRows.Add AlwaysInsert:=True
cdt,
Merci beaucoup Bono,, j'ai essayé votre syntaxe , mais elle ne marche pas, elle indiqué une erreur de compilation.
Si je ne me trompe, elle consiste juste à enlever les parenthèses de "alwaysinsert" = true".?
Bonjour,
Déclare tes variables LignTablo et LignTablob en 'ListRow'.
Je note quelques incohérences, mais difficile d'expliquer.
Un fichier serait le bienvenu pour t'apporter une aide adaptée.
Cdlt.