2016-03-03 99 views
1

已解决Nathan_SAV有正确的想法。对于任何人在看这个日后参考我改变了代码运行时错误91使用Range.find

For x = 3 To lRow 
On Error Resume Next 
'If then Find statement 
If Err.Number <> 0 Then 
'.... 
End If 

我还是新VBA和我打了一个错误,我无法弄清楚如何解决它。

我收到一个“运行时错误‘91’对象变量或带块变量未设置”

“获取此数据”表我在H列数字,我需要转移到“缺少数据”表单。此代码应该查看“缺失数据”表并在列H中查找空白。如果它找到空白,它将在“在此处获取数据”中搜索“缺失数据”中列A中单元格的值。然后它从“在此获取数据”中获取值并将其放入“缺失数据”中。我意识到这个描述很混乱,但如果你看看代码的意见应该帮助。

当我得到它是突出S =范围运行时错误(“A:A”)......

如果有人可以帮助我,我将不胜感激。

Dim x As Integer 
    Dim lRow As Long, S As Long 

    Sheets("Missing Data").Activate 
    'Find last row in Missing Data 
    lRow = Cells.Find(What:="*", _ 
     After:=Range("A1"), _ 
     LookAt:=xlPart, _ 
     LookIn:=xlFormulas, _ 
     SearchOrder:=xlByRows, _ 
     SearchDirection:=xlPrevious, _ 
     MatchCase:=False).Row 

    For x = 3 To lRow 
     'Search for all cells without an SAP# 
     If Cells(x, 8) = "" Then 
      Sheets("Get data here").Activate 
      Range("A1").Activate 
      'Set S equal to the row where the SQP# was found 
      S = Range("A:A").Find(What:=Sheets("Missing Data").Cells(x, 1), _ 
       After:=ActiveCell, _ 
       LookAt:=xlWhole, _ 
       LookIn:=xlFormulas, _ 
       SearchOrder:=xlByRows, _ 
       SearchDirection:=xlNext, _ 
       MatchCase:=False).Row 
      'Change the value of the blank cell to the value in column H 
      Sheets("Missing Data").Cells(x, 8).Value = Sheets("Get data here").Cells(S, 8) 
      'Change the cell color to yellow if it changed 
      If Sheets("Missing Data").Cells(x, 8) <> "" Then 
       Sheets("Missing Data").Cells(x, 8).Interior.ColorIndex = 6 
      End If 
     End If 
    Next x 
+1

添加集到该行的开头。在VBA中,你需要补充的是,如果你正在使用像Range对象那样的对象。 '设置S =范围(“A:A”)...' – Sobigen

+0

他不是他在使用.row –

+0

你是对的,请参阅Nathan的答案。 – Sobigen

回答

1

的数据可能不会被发现,所以尽量

Dim S as long 
Dim R as excel.range 
set R=Range("A:A").Find 
if R is nothing then 
    'Not found, handle 
else 
    S=R.row 
end if 
+0

这给出了一个编译错误,说“参数不是可选的”,突出显示 set R = Range(“A:A”)。 – BerticusMaximus