我刚开始使用VBA,在编码时遇到了一些问题。
我正在开发一种与RPA一起使用的解决方案,为此,我需要在电子表格中固定一个单元格,以保持单元格位置值由以下函数检索:
Function FindCell()
Dim CellLocation As Range
Set CellLocation = Sheets("Data").Range("A:A").Find("OBRC", LookIn:=xlValues) 'Returns the cell in which OBRC is found
并且为了满足我的需要,我实现了代码的第二部分,其中我的函数将在单元格P3中获取的值写入:
Function FindCell()
Dim CellLocation As Range
Set CellLocation = Sheets("Data").Range("A:A").Find("OBRC", LookIn:=xlValues) 'Returns the cell in which OBRC is found
While ActiveSheet.Name = "Data"
With ThisWorkbook.Sheets("Data").Range("P3").Value = FindCell.Value
End With
Wend
End Function
但是每次我尝试调用此函数时,都会出现错误
运行时错误424:需要对象
答案 0 :(得分:0)
首先,函数旨在返回一个值。对于上面的代码,过程将非常简单。
请注意在find函数中使用SearchDirection
来查找该值的最后一次出现。
Sub FindCell()
With Sheets("Data")
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim foundIt As Range
Set foundIt = .Range("A1:A" & lastRow).Find("OBRC", LookIn:=xlValues, SearchDirection:=xlPrevious)
If Not foundIt Is Nothing Then
.Range("P3").Value = foundIt.Address
End If
End With
End Sub
答案 1 :(得分:0)