简体   繁体   English

找不到对象错误:循环将在另一个工作表中找到具有相同值的单元格,选择新找到的单元格的偏移量

[英]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.

相关问题 如果单元格具有可以在另一个工作表中找到的相同值,则在单元格中写入随机值 - Write a random value in a cell if the cell has the same value that can be found in another sheet 在另一个工作表中查找单元格值 - Find cell value in another sheet 确定单元格值以在另一张工作表中找到相同的值 - Determine cell value to find in another sheet the same value 需要将单元格复制到另一张工作表,然后用新值覆盖(循环) - Need to copy cell to another sheet and then overwrite with new value (loop) 如果在同一行的另一个单元格中找到相同的值,则删除整行 - Delete entire row if the same value is found in another cell on the same row Excel,要根据其值查找一个单元格,然后从先前找到的单元格位置开始查找另一个单元格 - Excel, To find a cell based on its value then find another cell starting the search from the previously found cell position 如果工作表上的单元格值与另一张表上的单元格值相同,则删除该行 - Delete the row if the cell value on sheet is the same then the cell value on another sheet 循环遍历多个工作簿,然后匹配单元格值(如果找到),然后更新活动工作表中的不同单元格 - Loop thru multiple workbook then match cell value if found then update different cell in active sheet Select 基于在另一个单元格中找到的值的存在的单元格范围 - Select range of cells based on existence of a value found in another cell 搜索列中的每个单元格以查找特定值,并从该值所在的行中的另一个单元格复制值 - Search each cell in a column to find a certain value and copy value from another cell in the row the value is found in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM