VBA - Erreur d'execution
Bonjour,
Merci de m'aider à optimiser mon code ci-dessous.
Actuellement il m'affiche une erreur 1004 au niveau de "Selection.Offset(1, 0).Select"
Dim MyData, MyDataHead, MyDataStock As Object
Dim MyDataSave As Object
Dim mycol, myrow, i, j As Long
Dim rg As Variant
Dim reponse As Integer
Dim anneesel, moissel, moisannee As String
gerer_configuration
Set MyDataHead = Sheets("Inventaire").Range("B3").CurrentRegion
anneesel = MyDataHead.Range("C2")
moissel = MyDataHead.Range("B2")
If anneesel = "" Or moissel = "" Then
MsgBox "Merci de selectionner le mois et/ou l'année !", vbCritical, "Informations"
Else
Set MyDataStock = Sheets("Stock").Range("A1").CurrentRegion
moisannee = moissel & "/" & anneesel
Set rg = MyDataStock.Range("A1:A10000").Find(moisannee, MyDataStock.Range("A1"), LookIn:=xlValues, LookAt:=xlPart)
If Not rg Is Nothing Then
MsgBox "Un Inventaire a déja été défini pour cette période. Merci de selectionner une autre période !", vbCritical, "Informations"
Else
reponse = MsgBox("Voulez vous vraiment enregistrer cet inventaire ?", vbYesNo + vbQuestion, "Confirmation")
If reponse = vbYes Then
Set MyData = Sheets("Inventaire").Range("C7").CurrentRegion
mycol = MyData.Columns.Count
myrow = MyData.Rows.Count
Sheets("Stock").Activate
Range("A2").Select
Selection.End(xlDown).Select
For i = 2 To myrow - 1
For j = 2 To mycol - 1
If MyData(i, j) <> 0 Then
Selection.Offset(1, 0).Select
ActiveCell.Offset(0, 1).Value = moisannee
ActiveCell.Offset(0, 2).Value = MyData(1, j)
ActiveCell.Offset(0, 3).Value = MyData(i, 1)
ActiveCell.Offset(0, 4).Value = MyData(i, j)
End If
Next j
Next i
MsgBox "Inventaire enregistré avec succès !", vbInformation, "Informations"
restaurer_configuration
End If
End If
End If
Bonjour Black-Rabbit,
Ton erreur se situe au niveau de la sélection de ta ligne:
Range("A2").Select
Selection.End(xlDown).Select
essaie ainsi:
dl = Range("A" & Rows.Count).End(xlUp).Row + 1
Cells(dl, 1).Value = moisannee
Cells(dl, 2).Value = MyData(1, j)
Cells(dl, 3).Value = MyData(i, 1)
Cells(dl, 4).Value = MyData(i, j)
Option Explicit
Sub AjouterInventaire()
Dim MyData, MyDataHead, MyDataStock As Object
Dim MyDataSave As Object
Dim mycol, myrow, i, j As Long
Dim rg As Variant
Dim reponse As Integer
Dim anneesel, moissel, moisannee As String
Dim dl As Integer
Set MyDataHead = Sheets("Inventaire").Range("B3").CurrentRegion
anneesel = MyDataHead.Range("C2")
moissel = MyDataHead.Range("B2")
If anneesel = "" Or moissel = "" Then
MsgBox "Merci de selectionner le mois et/ou l'année !", vbCritical, "Informations"
Else
Set MyDataStock = Sheets("Stock").Range("A1").CurrentRegion
moisannee = moissel & "/" & anneesel
Set rg = MyDataStock.Range("A1:A10000").Find(moisannee, MyDataStock.Range("A1"), LookIn:=xlValues, LookAt:=xlPart)
If Not rg Is Nothing Then
MsgBox "Un Inventaire a déja été défini pour cette période. Merci de selectionner une autre période !", vbCritical, "Informations"
Else
reponse = MsgBox("Voulez vous vraiment enregistrer cet inventaire ?", vbYesNo + vbQuestion, "Confirmation")
If reponse = vbYes Then
Set MyData = Sheets("Inventaire").Range("C7").CurrentRegion
mycol = MyData.Columns.Count
myrow = MyData.Rows.Count
Sheets("Stock").Activate
dl = Range("A" & Rows.Count).End(xlUp).Row + 1
For i = 2 To myrow - 1
For j = 2 To mycol - 1
If MyData(i, j) <> 0 Then
Cells(dl, 1).Value = moisannee
Cells(dl, 2).Value = MyData(1, j)
Cells(dl, 3).Value = MyData(i, 1)
Cells(dl, 4).Value = MyData(i, j)
End If
Next j
Next i
MsgBox "Inventaire enregistré avec succès !", vbInformation, "Informations"
'
End If
End If
End If
End Sub
Cordialement,
Bonjour,
Incident résolu.
Merci de pour vos retours.
Spécial à xorsankukai !!!
Bonjour Black-Rabbit,
Ton erreur se situe au niveau de la sélection de ta ligne:
Range("A2").Select Selection.End(xlDown).Select
essaie ainsi:
dl = Range("A" & Rows.Count).End(xlUp).Row + 1 Cells(dl, 1).Value = moisannee Cells(dl, 2).Value = MyData(1, j) Cells(dl, 3).Value = MyData(i, 1) Cells(dl, 4).Value = MyData(i, j)
Option Explicit Sub AjouterInventaire() Dim MyData, MyDataHead, MyDataStock As Object Dim MyDataSave As Object Dim mycol, myrow, i, j As Long Dim rg As Variant Dim reponse As Integer Dim anneesel, moissel, moisannee As String Dim dl As Integer Set MyDataHead = Sheets("Inventaire").Range("B3").CurrentRegion anneesel = MyDataHead.Range("C2") moissel = MyDataHead.Range("B2") If anneesel = "" Or moissel = "" Then MsgBox "Merci de selectionner le mois et/ou l'année !", vbCritical, "Informations" Else Set MyDataStock = Sheets("Stock").Range("A1").CurrentRegion moisannee = moissel & "/" & anneesel Set rg = MyDataStock.Range("A1:A10000").Find(moisannee, MyDataStock.Range("A1"), LookIn:=xlValues, LookAt:=xlPart) If Not rg Is Nothing Then MsgBox "Un Inventaire a déja été défini pour cette période. Merci de selectionner une autre période !", vbCritical, "Informations" Else reponse = MsgBox("Voulez vous vraiment enregistrer cet inventaire ?", vbYesNo + vbQuestion, "Confirmation") If reponse = vbYes Then Set MyData = Sheets("Inventaire").Range("C7").CurrentRegion mycol = MyData.Columns.Count myrow = MyData.Rows.Count Sheets("Stock").Activate dl = Range("A" & Rows.Count).End(xlUp).Row + 1 For i = 2 To myrow - 1 For j = 2 To mycol - 1 If MyData(i, j) <> 0 Then Cells(dl, 1).Value = moisannee Cells(dl, 2).Value = MyData(1, j) Cells(dl, 3).Value = MyData(i, 1) Cells(dl, 4).Value = MyData(i, j) End If Next j Next i MsgBox "Inventaire enregistré avec succès !", vbInformation, "Informations" ' End If End If End If End Sub
Copie de Classeur3-3.xlsm
Cordialement,