简体   繁体   English

使用VBA中的“查找”功能查找活动单元上方的第一个值?

[英]Use the Find function in VBA to find the first value above the active cell?

I am trying to get the row number of the result of a find function, I am using the following. 我正在尝试获取查找功能的结果的行号,我正在使用以下内容。

Set FindRow = ThisWorkbook.Sheets("Schedule").Range(ActiveCell.Address & ":n" & Sheets("Schedule").Cells(ActiveSheet.Rows.Count, 1).End(XlUP).Row).Find(What:="Assembly", SearchDirection:=xlPrevious, MatchCase:=False)

Msgbox (FindRow.Row)

This works fine but returns the last cell in the range that has the value in it. 这可以正常工作,但返回其中包含值的范围中的最后一个单元格。 If I alter the Search Direction to XlNext, this returns the next cell below with the value in it. 如果将“搜索方向”更改为XlNext,这将返回下面的下一个单元格,其中包含值。 I would like it to search above the active cell for the next value. 我希望它在活动单元格上方搜索下一个值。

Think you just need to specify the After parameter. 认为您只需要指定After参数即可。 Edit - on reflection depending on active cell the range you are searching could be any size so you need to explain what you mean by "above the active cell". 编辑-根据活动单元的反射情况,您要搜索的范围可以是任意大小,因此您需要解释“活动单元上方”的含义。

Sub x()

Dim FindRow As Range, r As Range

With ThisWorkbook.Sheets("Schedule")
    Set r = .Range(ActiveCell.Address & ":N" & .Cells(.Rows.Count, 1).End(xlUp).Row)
    Set FindRow = r.Find(What:="Assembly", after:=r(r.Cells.Count), SearchDirection:=xlNext, MatchCase:=False)
End With

If Not FindRow Is Nothing Then MsgBox (FindRow.Row)

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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