ist Bruce Wayne die Antwort auf Ihre Frage, die folgenden können Ihnen helfen, mögliche künftige Probleme zu vermeiden:
Public Sub main()
Dim maxVal As Long
Dim origValue As Long
Dim FoundCell As Range
With Worksheets("Complications") '<--| reference this sheet once and for all and rest assure you're dealing with it if not otherwise explicitly referenced
maxVal = .Range(.Rows.Count, 1).End(xlUp).Row '<--| find the "real" last non blank cell in column A, should any blank cell precede before it
For origValue = 2 To maxVal
Set FoundCell = .Range("A1", "A50000").Find(What:=.Cells(origValue, 1).Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) '<--| always specify those 4 Find() method parameters
If FoundCell Is Nothing Then
.Cells(origValue, 1).Value = Sheets("Cases").Cells(origValue, 1).Value
Else
End If
Next
End With
End Sub
Kommentar auf Find()
Methode beruht auf der Tatsache, dass jede Nutzung der es (auch aus Excel UI) setzt diese Parameter als Standard für die spätere Verwendung. Es ist also besser, immer genau anzugeben, was Sie wirklich brauchen.
schließlich sollte es keine Else
Klausel den Code zu behandeln, dann könnte Zusammenbruch zu
Public Sub main2()
Dim maxVal As Long
Dim origValue As Long
With Worksheets("Complications") '<--| reference this sheet once and for all and rest assure you're dealing with it if not otherwise explicitly referenced
maxVal = .Range(.Rows.Count, 1).End(xlUp).Row '<--| find the "real" last non blank cell in column A, should any blank cell precede it
For origValue = 2 To maxVal
If .Range("A1", "A50000").Find(What:=.Cells(origValue, 1).Value, LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=False) Is Nothing Then .Offset(origValue - 1).Value = Sheets("Cases").Cells(origValue, 1).Value '<--| always specify those 4 parameters of Find() method
Next
End With
End Sub
Falsche Verwendung von 'Range (a1, a50000)' – dbmitch
Um die Antworten hier auf Englisch zu erklären ... Find gibt ein Bereichsobjekt zurück, und Sie haben FoundCell korrekt als einen Bereich deklariert, bei dem es sich um ein Objekt handelt. Eine Besonderheit von VBA ist, dass Objekte mit dem Schlüsselwort 'Set' und nicht nur mit object = was auch immer zugewiesen werden müssen. Verwenden Sie daher 'Set' FoundCell = anstatt nur FoundCell =. Hier ist eine gute Erklärung http://www.homeandlearn.org/the_set_keyword.html – Absinthe