我试图在与运行代码不同的封闭文件中找到包含某些值的单元格。找到它后,我想接收所述单元格的行号。不知何故我的代码无法正常工作。它是我在本网站上发现的东西和我自己编写的东西的混合体。任何帮助将是巨大的!谢谢。
Dim file_dir As Workbook
Set file_dir = Workbooks.Open("PATH.xlsx")
Dim j As Integer
Dim n As Integer
Dim temp As Range
n = file_dir.ActiveSheet.Cells(file_dir.ActiveSheet.Cells.Rows.Count, "A").End(xlUp).Row
Set temp = file_dir.ActiveSheet.Range("A1:A" & n).Find(what:="LOOKUP_VALUE")
j = temp.Row
MsgBox j
答案 0 :(得分:0)
欢迎您使用Stack Overflow,我可以看到您是一个初学者,所以我认为我将展示一种解决此问题的不同方法。您需要学习数据类型和返回的函数,所以我认为与其使用嵌套函数等,不如从一个简单的代码开始,然后通过使其运行更快等来使其更复杂,这是更好的选择。看来您的价值是在A列中,因此无需循环使用FIND
并将其与您的lookup_value
进行比较。确保在例程中为lookup_value
提供了一个真实值:
Sub Main()
Dim bValueFound As Boolean
Dim j As Integer
Dim n As Integer
Dim temp As Range
Dim WS As Worksheet
Dim file_dir As Workbook
Set file_dir = Workbooks.Open("PATH.xlsx")
Set WS = file_dir.ActiveSheet
n = WS.Cells(WS.Cells.Rows.Count, "A").End(xlUp).Row
Set temp = file_dir.ActiveSheet.Range("A1:A" & n)
For j = 1 To n
If WS.Cells(j, 1).Value = LOOKUP_VALUE Then
bValueFound = True
Exit For
End If
Next j
If bValueFound Then
MsgBox "The row is " & j
Else
MsgBox "Lookup value was not found"
End If
End Sub