Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从列表中查找多个单元格值_Excel_Vba - Fatal编程技术网

Excel 从列表中查找多个单元格值

Excel 从列表中查找多个单元格值,excel,vba,Excel,Vba,我有一本两页的工作簿。 表1包含a列中的产品代码列表,R列为当前库存水平。 表2包含a列中的产品代码列表,B列包含新库存水平。 我要做的是将Sheet1中的当前库存水平替换为Sheet2中的新库存水平 我在这个网站上已经找到了一些代码(如下),我已经为我的目的稍微修改了一些代码,它工作得很好,但只适用于一个产品代码(因为它引用了A1和B1)。我想做的是添加一个循环,这样它就可以运行Sheet2中的所有产品,但我不知道如何进行,也无法适应我在网上找到的任何类似循环 任何帮助都将不胜感激,我的备份计

我有一本两页的工作簿。
表1包含a列中的产品代码列表,R列为当前库存水平。 表2包含a列中的产品代码列表,B列包含新库存水平。 我要做的是将Sheet1中的当前库存水平替换为Sheet2中的新库存水平

我在这个网站上已经找到了一些代码(如下),我已经为我的目的稍微修改了一些代码,它工作得很好,但只适用于一个产品代码(因为它引用了A1和B1)。我想做的是添加一个循环,这样它就可以运行Sheet2中的所有产品,但我不知道如何进行,也无法适应我在网上找到的任何类似循环

任何帮助都将不胜感激,我的备份计划是在Sheet1中进行v形查找,以引入Sheet2新的库存水平值,然后替换原始列,但如果可能,我希望以其他方式工作

Private Sub CommandButton1_Click()

    Dim search_range As Range, search_value As Range, _
    lastcell As Range, foundcell As Range
    Dim ws As Worksheet


Set ws = ThisWorkbook.Sheets("Sheet1")
Set search_range = ws.Range("A1", ws.Range("A" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)
Set search_value = ThisWorkbook.Sheets("Sheet2").Range("A1")

Set foundcell = search_range.Find(What:=search_value, After:=lastcell, 
LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)

If Not foundcell Is Nothing Then foundcell.Activate Else MsgBox "Not Found"

ActiveCell.Offset(0, 17).Value = Sheets("Sheet2").Range("B1").Value



End Sub

那么以下内容如何:

Private Sub CommandButton1_Click()

Dim search_range As Range, search_value As Range, lastcell As Range, foundcell As Range
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet1")
Set search_range = ws.Range("A1", ws.Range("A" & Rows.Count).End(xlUp))
Set lastcell = search_range.Cells(search_range.Cells.Count)

For i = 1 To lastcell.Row

    Set search_value = ThisWorkbook.Sheets("Sheet2").Range("A" & i)
    Set foundcell = search_range.Find(What:=search_value, After:=lastcell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

    If Not foundcell Is Nothing Then foundcell.Activate Else MsgBox "Not Found"
    ActiveCell.Offset(0, 17).Value = Sheets("Sheet2").Range("B" & i).Value

Next i

End Sub

这个想法是这样的-你有两种类型的范围-你搜索的范围和你的值应该在的范围。它们被称为
Target
Search

在下面的代码中,您循环遍历第一个工作表的
A
列中的所有单元格,并在第二个工作表的
A
列中查找它们的值。如果找到该值,则将第二个工作表的
B列中的值写入17。第一个工作表中的列:

Private Sub CommandButton1_Click()

    Dim targetRange     As Range
    Dim targetValue     As Range
    Dim searchRange     As Range        
    Dim lastSearchCell  As Range
    Dim foundCell       As Range        
    Dim wsTarget        As Worksheet
    Dim wsSearch        As Worksheet

    Dim myCell          As Range

    Set wsTarget = ThisWorkbook.Worksheets(1)
    Set wsSearch = ThisWorkbook.Worksheets(2)

    With wsTarget
        Set targetRange = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
    End With

    With wsSearch
        Set searchRange = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
    End With

    Set lastSearchCell = searchRange.Cells(searchRange.Cells.Count)

    For Each myCell In targetRange
        Set foundCell = searchRange.Find(What:=myCell, After:=lastSearchCell).Offset(0, 1)
        If Not foundCell Is Nothing Then
            myCell.Offset(0, 17) = foundCell
        Else
            MsgBox "Not Found"
        End If
    Next myCell

End Sub

您好,在“i=1到lastcell”行的“运行时错误13”错误。我是否需要添加lastcell2,它指的是sheet2,因为我认为第一个指的是Sheet1?只需再次粘贴它,它就工作了!对不起,不知道第一次失败的原因:)非常感谢!感谢您的回答,很高兴有几个选项可以帮助我提高知识:)