我正在尝试创建一些vba,它将在工作表1的列中的单元格上执行循环,然后检查该单元格在工作表2上是否明显,如果是,则我希望将工作表2值的偏移粘贴到原始工作表1值旁边。我获取要查找的单元格函数或值所需的错误对象。
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
您的代码中有很多东西看起来有点奇怪(根据有效语法(,所以我在下面重新编写了一个示例。
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"添加到第一个单元格循环