这是我的程序,我是VBA的新手,我总是发现努力解决这种类型的错误。
请帮帮我。
Sub findsub()
Dim t As Range
Dim j As Range
Set j = Range("j1")
If j Is Nothing Then
Debug.Print "please enter the number"
Else
Set t = Range("G2:G19").Find("j1", LookIn:=xlValues)
Range("J4").Value = t.Offset(0, 1).Value '<-- ERROR HERE
End If
End Sub
答案 0 :(得分:1)
j
不能是Nothing,因为在您的代码中,您将其分配给此处的内容:
Set j = Range("j1")
相反,t
如果未通过Nothing
找到.Find
因此:
Option Explicit
Sub findsub()
Dim t As Range
Dim j As Range
Set j = Range("j1")
Set t = Range("G2:G19").Find("j1", LookIn:=xlValues)
If Not t Is Nothing Then
Range("J4").Value = t.Offset(0, 1).Value
End If
End Sub
长话短说,在网上搜索&#34; NULL,EMPTY,NOTHING,MISSING vba&#34;并阅读一些文章。像这个: http://allenbrowne.com/vba-NothingEmpty.html
答案 1 :(得分:1)
可能是你这样的事情(评论中的解释)
Option Explicit
Sub findsub()
Dim t As Range, j As Range
Set j = Range("J1")
If IsEmpty(j) Then ' if cell J1 is empty
Debug.Print "please enter the number in cell J1"
Else
Set t = Range("G2:G19").Find(j.Value, LookIn:=xlValues, LookAt:=xlWhole) ' always explicitly define both LookIn and LookAt parameters otherwise those from last Find call (even from UI) will be used
If Not t Is Nothing Then Range("J4").Value = t.Offset(0, 1).Value ' write in J4 if successfully found J1 value in range G2:G19
End If
End Sub