L4zl0w
L4zl0w

Reputation: 1097

Excel VBA: setting cell value based on a search

I'm trying to set the ActiveCell's value as follows:

This is my code, I get an exception without any helpful information.

ActiveCell.Value = Cells.Find(What:=ActiveCell.Offset(-1, 0).Value, 
After:=Sheets("Source - Questions").Cell(1000, 3), LookIn:=xlFormulas, 
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, 
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Value

Any help would be appreciated.

Cheers

Upvotes: 0

Views: 207

Answers (1)

Gary's Student
Gary's Student

Reputation: 96753

Correct the two small errors:

Sub asdf()
    Dim r As Range, s As Worksheet, v As Variant
    Set s = Sheets("Source - Questions")
    v = ActiveCell.Offset(-1, 0).Value
    Set r = s.Cells.Find(What:=v, _
        After:=s.Cells(1000, 3), _
        LookIn:=xlFormulas, _
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlPrevious, _
        MatchCase:=False, _
        SearchFormat:=False).Offset(1, 0)
    ActiveCell.Value = r.Value
End Sub

Upvotes: 1

Related Questions