Ich habe ein Problem mit meinem Code in Excel 2016, VBA.Warum bricht For ... Next Loop nach 2 Run ab?
haben Sie einen Blick auf den Code:
Private Sub Add_ProjectName()
Dim i As Integer
Dim iRowName As Integer
Dim iColName As Integer
Dim rFind As Range
Dim iFind As Long
Dim ws As Worksheet
Dim lRow As Integer
'Find last row in Master Sheet
Set ws = ThisWorkbook.Worksheets("Master")
ws.Activate
lRow = ws.Cells(ws.Rows.Count, 7).End(xlUp).Row
'Start adding project names per day
With ws
For i = 6 To lRow
Set rFind = .Range(.Cells(5, 14), .Cells(5, 378)) 'Each cell in this range is a date ranging from Feb 1 to Dec 31
rFind.NumberFormat = "mm-dd" 'Change the Format of the Date Range from "dd" to "mm-dd"
iFind = .Cells(i, 4).Value 'The Commencement date of the Project
'Find the Column of the Date that is equal to the Commencement date of the Project on rFind
iColName = rFind.Find(What:=Format(CDate(iFind), "mm-dd"), _
After:=.Range(.Cells(5, 14), .Cells(5, 14)), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True).Column
'Set the Row of the Commencement date of the Project
iRowName = .Cells(i, 10).Row
'Adding the Project Name
.Cells(iRowName, iColName).Value = .Cells(i, 10).Value
Set rFind = Nothing
Next i
End With
'Change the format of the whole range back to showing the Date only
rFind.NumberFormat = "dd"
End Sub
Also, das läuft gut für die ersten beiden Iterationen. Dann, wenn die dritte Iteration startet, was ich bekomme, war "Laufzeitfehler '91' - Objektvariable oder Mit Blockvariable nicht gesetzt".
Nach Debugging, sagte das System, dass es von
iColName = rFind.Find(What:=Format(CDate(iFind), "mm-dd"), _
After:=.Range(.Cells(5, 14), .Cells(5, 14)), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True).Column
ich dies überprüfen immer wieder verursacht wurde und nicht finden kann, die „Mit Block nicht festgelegt“ noch Unset Objekt.
Hat jemand eine Idee, wie man das beheben kann?
Vielen Dank im Voraus.
Thi An.
Welche Aussage löst den Fehler aus? – trincot
Der Teil, wo ich die Spalte des Datums finden muss, iColName-Variable, um genau zu sein. Ich werde die Frage bearbeiten. Danke, dass du darauf hingewiesen hast. –
Das bedeutet, dass die Methode 'Find' keine Übereinstimmung gefunden hat. Probieren Sie 'Debug.Print' aus, nach dem Sie vor der Suche suchen. –