excel vba:找到具有特定值的单元格行,并将该行号放在另一个单元格中

时间:2016-03-21 02:47:21

标签: excel vba excel-vba

我正在尝试找到具有特定值(与range("C1").value相同的值)的单元格行,并将该行号放在另一个单元格中。我坚持了两个小时。

我的代码如下: (之前我激活了sheet1)

Set found = sheet1.Columns("B").Find(what:= sheet1.range("C1").Value, LookIn:=xlValues, lookat:=xlWhole)       
sheet1.range("A2").value= found.Address  

此代码收到错误消息Object variable not set

此错误的来源是什么?

1 个答案:

答案 0 :(得分:0)

发现变量的B'cos是空的,因为Find()没有找到任何匹配的值,也没有在找到的变量中设置任何内容。

Dim found As Range

Set found = Sheet1.Columns("B").Find(what:=Sheet1.Range("C1").Value, LookIn:=xlValues, lookat:=xlWhole)

If Not found Is Nothing Then
    Sheet1.Range("A2").Value = found.Address
End If
相关问题