[英]Object not found Error: loop that will find the cell with the same value in another sheet, select the offset of the new found cell
I am trying to create some vba that will do a loop on cells in a column on sheet 1, then check if the cell is apparent on sheet 2, if it is then i want the offset of the sheet 2 value pasted next to the original sheet 1 value.我正在尝试创建一些 vba,它将对工作表 1 上的列中的单元格进行循环,然后检查该单元格是否在工作表 2 上可见,如果是,则我希望将工作表 2 值的偏移量粘贴到原始值旁边表 1 值。 I get the error object required on the cell function or value to find.我得到要查找的单元格函数或值所需的错误对象。
Sub findValue()
Dim xlRange As Range
Dim xlCell As Range
Dim xlSheet As Worksheet
Dim valueToFind
For Each cell In Range("h2:h8")
cell.Select
cell = ActiveCell
MsgBox (cell)
valueToFind = ActiveCell
Set xlSheet = ActiveWorkbook.Worksheets("DATA")
Set xlRange = xlSheet.Range("A1:A13")
For Each xlCell In xlRange
If xlCell.Value = valueToFind Then
MsgBox (xlCell.Offset(0, 1).Value)
valueToFind.Offset(0, 2).Value = xlCell.Offset(0, 1).Value
End If
Next xlCell
There's a lot of stuff that looks a little strange (according to what is valid syntax) in your code, so I re-wrote an example below.您的代码中有很多看起来有点奇怪的东西(根据有效语法),所以我在下面重新编写了一个示例。
Sub getOffsetValue()
Dim origWS as Worksheet
Dim dataWS as Worksheet
Set origWS = Worksheets("original") 'change as needed
Set dataWS = Worksheets("DATA")
Dim checkCell as Range
For each checkCell in origWS.Range("H2:H8")
Dim foundIt as Range
Set foundIt = dataws.Range("A1:A13").Find(checkCell.Value)
If Not foundIt is Nothing then
MsgBox foundIt.Offset(0, 1).Value
checkCell.Offset(0, 2).Value = foundIt.Offset(0, 1).Value
End If
Next
End Sub
答案:我需要在第一个单元格循环中添加“set”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.