简体   繁体   中英

Excel VBA: setting cell value based on a search

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

  • I need to look at the value of the cell that is one row above the active cell (in the same column)
  • I then need to find this value in the 3rd column of the 'Source - Questions' sheet; I need to start the search from the bottom going up because I need to find the last instance of this value
  • When I find this cell I need to take the value of the next cell that is just under the cell that was found (in the same column)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM