2017-03-18 3 views
0

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.

+2

Welche Aussage löst den Fehler aus? – trincot

+0

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. –

+2

Das bedeutet, dass die Methode 'Find' keine Übereinstimmung gefunden hat. Probieren Sie 'Debug.Print' aus, nach dem Sie vor der Suche suchen. –

Antwort

0

Ich denke, dass Sie versuchen, eine Zeichenfolge (zurückgegeben von format) in einem Bereich zu finden, der enthält. Ich würde versuchen

iColName = rFind.Find(What:=iFind, 

stattdessen.

+0

Ich habe das versucht Ich konnte es nicht ausführen und fand heraus, dass es nicht mit den Elementen in rFind übereinstimmte, da ich rFind auf "mm-dd" formatierte, daher das passende Format, in dem ich es verwendet habe 'rFind.NumberFormat =" mm-dd "' und 'Format (CDate (iFind)," mm-dd ").. –

+0

@ThiAn ändert' rFind.NumberFormat' wird seinen Wert für die Suche nicht ändern. –

0

Ich habe es heute noch einmal versucht und herausgefunden, dass das Problem mit dem Parameter LookIn:= war. Ich änderte es von LookIn:=xlValues zu LookIn:=xlFormulas und es funktionierte!

Ich musste es ein wenig durch eine andere Zeile (in diesem Fall Zeile (1)) zwicken und alle Daten zurück in "General" Format (z. B. 42736 für 1. Januar 2017).

Ich habe einen Error Handler hinzugefügt, falls rFind Nothing zurückgibt.

Vielen Dank für Ihre Hilfe @iDevlop und @ A.S.H

Hier ist der Arbeitscode.

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 


     iFind = .Cells(i, 4).Value  'The Commencement date of the Project 

     'This is 365 days in a year shown in "General" Format 
     Set rFind = .Range(.Cells(1, 14), .Cells(1, 378)).Find(What:=iFind, _ 
                    After:=.Cells(1, 13), _ 
                    LookIn:=xlFormulas, _ 
                    LookAt:=xlWhole, _ 
                    SearchOrder:=xlByRows, _ 
                    SearchDirection:=xlNext, _ 
                    MatchCase:=True) 

     If rFind Is Nothing Then 
      GoTo NextIteration 
     Else 
      iColName = rFind.Column   'Set the Column of the Commencement date of the Project 
      iRowName = .Cells(i, 10).Row 'Set the Row of the Commencement date of the Project 

      'Adding the Project Name 
      .Cells(iRowName, iColName).Value = .Cells(i, 10).Value 
     End If 
NextIteration: 
    Next i 

End With 

End Sub 
+0

Definitiv @Ralph.Ich kann jetzt nicht abstimmen, aufgrund meines Rufes, auf den Sie hingewiesen haben. Aber ich werde in der Zukunft. Natürlich kann ich meine eigene Antwort nicht akzeptieren, also ... Danke, dass du mich informiert hast. –