J'ai un code Lmonth Loop qui recherche dans une liste de dates pour trouver les travaux qui ont été commandés en janvier (1) par exemple et les copie et les colle dans une nouvelle feuille.
Le code fonctionne bien mais quand il arrive alors end it signale une erreur @debug 13 '
Si je désactive la ligne, le code ne fonctionne pas mais je ne peux pas déterminer ce qui est cassé.
Sub Search_Month()
Dim datasheet As Worksheet
Set datasheet = Sheet2
Dim Mreport As Worksheet
Set Mreport = Sheet9
Dim Lmonth As Integer
Search = Range("m4").Value
Dim i As Integer
Mreport.Unprotect Password:=rapid1
Mreport.Range("a2:a300").ClearContents
datasheet.Activate
For i = 7 To 5000
Lmonth = Month(Cells(i, 6))
If Lmonth = Search Then
Range(Cells(i, 2), Cells(i + 3, 2)).Copy
Mreport.Activate
Range("A1000").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
datasheet.Activate
End If
Next i
Mreport.Activate
Mreport.Protect Password:=rapid1
MsgBox "End of Month Report Updated"
End Sub
La ligne qui marque est Lmonth = Month (Cells (i, 6)) mais je ne sais pas pourquoi.
Tous les résultats trouvés par la macro sont corrects, juste l'erreur à la fin est ennuyeuse. Je pense que cela dit "la prochaine ligne de recherche n'affiche pas lmonth = 1, donc je ne peux plus exécuter le code donc il doit être cassé"
3 Réponses :
L'erreur de débogage 13 est une incompatibilité de type. Ainsi, la fonction "Mois" reçoit une valeur qui ne peut pas être traitée.
Voir Documentation < / a> pour référence. Il doit s'agir d'une date.
Votre problème principal semble le fait que vous passez simplement de la ligne 7 à la ligne 5000 sans même vérifier s'il y a du contenu. Je ne pense pas que vous puissiez vous fier au fait qu'il y a toujours 4993 entrées dans le tableau.
Je recommande donc de changer la boucle en quelque chose comme For i = 7 To ActiveSheet.UsedRange.Rows.Count . Vous pouvez également vérifier le type de données avant d'utiliser "Month ()" avec le " IsDate " -fonction si vous n'êtes pas sûr.
la seule raison pour laquelle j'ai utilisé 7-5000 était parce que je ne pouvais pas faire fonctionner le codage de plage utilisée et je savais que 5000 était un nombre sûr (chaque travail prend 4 lignes) car environ 1200 emplois en un an correspondent au maximum que nous recevons. Mon problème est que j'ai très peu d'expérience avec VBA, donc mes codes sont souvent simples et longs.
Vous devriez également consulter un didacticiel sur Validation des données - les mauvaises dates appartiennent donc au passé.
rapid1 en une chaîne. Tu pourrais vouloir
modifiez-le pour que le code fonctionne. Sub Search_Month_No_Constants()
' Data
Dim datasheet As Worksheet ' Worksheet
Dim rng As Range ' Last Cell Range
Dim Search As Long ' Search Month
Dim vntMonth As Variant ' Current Month
Dim i As Long ' Row Counter
' Report
Dim Mreport As Worksheet ' Worksheet
Dim FER As Long ' First Empty Row
' Create References to Worksheets
Set datasheet = Sheet2
Set Mreport = Sheet9
' Speed up
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
On Error GoTo ProcedureExit
' In Data Worksheet
With datasheet
' Assign value from Search Value Cell Range to Search Month.
Search = .Range("M4").Value
' In Search Column
With .Columns("F")
' Calculate Last Cell Range in Search Column.
Set rng = .Find("*", , xlFormulas, xlWhole, xlByColumns, xlPrevious)
End With
If rng Is Nothing Then ' No data in column (Highly unlikely).
MsgBox "No Data in column 'F'." _
GoTo ProcedureExit
End If
' In Report Worksheet
With Mreport
.Unprotect Password:="rapid1"
' Clear contents from First Row to bottom cell of Write Column.
.Cells(2, "A").Resize(.Rows.Count - 2 + 1).ClearContents
' Write First Row Number to First Empty Row.
FER = 2
End With
' Loop through cells of Data Worksheet.
For i = 7 To rng.Row
' Write value of current cell to Current Month.
vntMonth = .Cells(i, "F")
' Check if Current Month is a date or can be converted to a date.
If IsDate(vntMonth) Then
' Check if month of current cell value is equal to Current Month.
If Month(vntMonth) = Search Then
' Write data from Data Worksheet to Report Worksheet.
Mreport.Cells(FER, "A").Resize(3) = _
.Cells(i, "B").Resize(3).Value
FER = FER + 3
End If
End If
Next
End With
' In Report Worksheet
With Mreport
.Protect Password:="rapid1"
MsgBox "End of Month Report Updated"
End With
ProcedureExit:
' Speed down
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Sub Search_Month()
' Data
Const cSearch As String = "M4" ' Search Value Cell Range
Const cFRD As Long = 7 ' First Row Number
Const cOffset As String = 3 ' Copy Row Offset
Const cCol As Variant = "F" ' Search Column Letter/Number
Const cCopy As Variant = "B" ' Copy Column Letter/Number
' Report
Const cFRR As Long = 2 ' First Row Number
Const cWrite As Variant = "A" ' Write Column Letter/Number
' Data
Dim datasheet As Worksheet ' Worksheet
Dim rng As Range ' Last Cell Range
Dim Search As Long ' Search Month
Dim vntMonth As Variant ' Current Month
Dim i As Long ' Row Counter
' Report
Dim Mreport As Worksheet ' Worksheet
Dim FER As Long ' First Empty Row
' Create References to Worksheets
Set datasheet = Sheet2
Set Mreport = Sheet9
' Speed up
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
On Error GoTo ProcedureExit
' In Data Worksheet
With datasheet
' Assign value from Search Value Cell Range to Search Month.
Search = .Range(cSearch).Value
' In Search Column
With .Columns(cCol)
' Calculate Last Cell Range in Search Column.
Set rng = .Find("*", , xlFormulas, xlWhole, xlByColumns, xlPrevious)
End With
If rng Is Nothing Then ' No data in column (Highly unlikely).
MsgBox "No Data in column '" _
& Split(.Cells(1, cCol).Address, "$")(1) & "'."
GoTo ProcedureExit
End If
' In Report Worksheet
With Mreport
.Unprotect Password:="rapid1"
' Clear contents from First Row to bottom cell of Write Column.
.Cells(cFRR, cWrite).Resize(.Rows.Count - cFRR + 1).ClearContents
' Write First Row Number to First Empty Row.
FER = cFRR
End With
' Loop through cells of Data Worksheet.
For i = cFRD To rng.Row
' Write value of current cell to Current Month.
vntMonth = .Cells(i, cCol)
' Check if Current Month is a date or can be converted to a date.
If IsDate(vntMonth) Then
' Check if month of current cell value is equal to Current Month.
If Month(vntMonth) = Search Then
' Write data from Data Worksheet to Report Worksheet.
Mreport.Cells(FER, cWrite).Resize(cOffset) = _
.Cells(i, cCopy).Resize(cOffset).Value
FER = FER + cOffset
End If
End If
Next
End With
' In Report Worksheet
With Mreport
.Protect Password:="rapid1"
MsgBox "End of Month Report Updated"
End With
ProcedureExit:
' Speed down
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Eh bien, c'est embarrassant ...
J'ai essayé plusieurs de vos options ci-dessus en vain, je suis ensuite allé coller quelque chose dans mon code et il a collé un numéro de travail que je pensais étrange, mais qui était en fait le travail non de l'endroit où le code était en panne. Alors je suis allé regarder ce champ et j'ai trouvé que j'avais entré la date pour ce champ comme 07/02/19 /, le disjoncteur avant final lançait une erreur de code.
Supprimé le / et réexécutez le code et cela a parfaitement fonctionné sans erreur de débogage.
Merci à tous pour votre aide et vos conseils, j'utiliserai votre codage et vos commentaires pour améliorer ce code et bien d'autres à l'avenir
Encore merci!
Quelle est la valeur de
iet le contenu deCells (i, 6)en cas d'erreur?La valeur de i varie mais le sera lorsque la date recherchée n'est plus égale au numéro du mois recherché. Par exemple, si je recherche tous les emplois en janvier et que i82 est 01/02/19 (cellules (1,6), il y aura une erreur à la ligne 82.
Je ne peux pas reproduire votre erreur avec les informations et le code que vous avez fournis. Si vous ne trouvez pas d'autre solution, je vous suggère de télécharger un classeur (avec les informations sensibles supprimées) qui démontre le problème sur un site de partage (par exemple, dropbox, onedrive), puis poster un lien ici.