我想创建一个接受两个输入变量InputVal的函数,该输入变量始终为数字,而RefCell始终为一个单词。然后,该函数应使用RefCell中的单词在工作簿的另一张Sheet中搜索某个单元格。然后从“找到的单元格”向右移动四个空格,并获取其中的内容(也始终为数字),然后将其乘以InputVal并返回结果。
Function FindAndCalc(InputVal As Range, RefCell As Range)
Dim FindCell As Range, NewCell As Range
FindCell = Worksheets("Sheet1").Cells.Find(what:=RefCell.Value)
NewCell = Worksheets("Sheet1").FindCell.Offset(0, 4)
FindAndCalc = NewCell.Value * InputVal.Value
End Function
当前,它只是返回一个错误“ #VALUE!”,而它应该返回一个数值。
答案 0 :(得分:3)
您可能已经错过了一些Set
了。也刚刚添加了一些逻辑
Function FindAndCalc(InputVal As Range, RefCell As Range)
Dim FindCell As Range, NewCell As Range
Set FindCell = Worksheets("Sheet1").Cells.Find(RefCell.Value)
If Not FindCell is Nothing Then
Set NewCell = FindCell.Offset(0, 4)
End If
FindAndCalc = NewCell.Value * InputVal.Value
End Function