简体   繁体   English

Excel项目名称搜索匹配-2个打开文件

[英]Excel Item Name Search Match - 2 Open Files

I have been trying to approach a particular case, but have yet to know where to go from what I have so far. 我一直在尝试处理一个特定的案例,但是到目前为止我还不知道该从何而来。 Any help would be greatly appreciated. 任何帮助将不胜感激。

I found something which I wanted to try to see if I could build from it to my particular case here. 我找到了一些我想尝试的东西,以查看是否可以在这里针对我的特定案例进行构建。 I copied the contents from file2 to sheet3 instead of using two separate files to see if this too could work. 我将内容从file2复制到sheet3,而不是使用两个单独的文件来查看是否也可以工作。 But, after changing values around, I still got an error(Run time error 9. Subscript out of range) 但是,在改变值之后,我仍然遇到错误(运行时错误9。下标超出范围)

Originally, I have two excel files. 最初,我有两个Excel文件。

File1 has item Names in columnE starting after A5 in sheet2. File1在sheetE中的A5之后,在columnE中具有名称项。 File2 has item Names in columnA starting after A3 in sheet1. File2在sheet1的A3之后的columnA中具有名称项。

I want to write a macro that can place a number(Barcode) into ColumnD in file2, found from ColumnD in File1, for each cell value in ColumnA from file2 that matches cell value in ColumnE from file1. 我想编写一个宏,该宏可以将file2中ColumnA中每个单元格值与file1中ColumnE中的单元格值相匹配的宏(条形码)放入File2中ColumnD中找到的file2中的ColumnD。

In other words, File2 with item names in columnA that have no bar code in columnD, search list of all items in File1 ColumnE, find exact name match, from that same row copy value of column D containing barcode, paste barcode value into File2 empty columnD. 换句话说,在columnA中具有项目名称的File2在columnD中没有条形码,在File1 ColumnE中搜索所有项目的列表,找到完全匹配的名称,从包含条形码的D列的同一行复制值中,将条形码值粘贴到File2空中D列。

Sub Find_Barcode()

Dim PartRngSheet1 As Range, PartRngSheet2 As Range
Dim lastRowSheet1 As Long, lastRowSheet2 As Long
Dim cl As Range, rng As Range

lastRowSheet1 = Worksheets("Sheet2").Range("E65536").End(xlUp).Row
Set PartRngSheet1 = Worksheets("Sheet2").Range("A1:A" & lastRowSheet1)

lastRowSheet2 = Worksheets("Sheet3").Range("A65536").End(xlUp).Row
Set PartRngSheet2 = Worksheets("Sheet3").Range("A1:A" & lastRowSheet2)

For Each cl In PartRngSheet1
    For Each rng In PartRngSheet2
        If (cl = rng) Or (cl = rng.Offset(0, 1)) Then
            rng.Offset(0, 4) = cl.Offset(0, 1)
        End If
    Next rng
Next cl

End Sub

Try below code : Assuming File1 & File2 are two different workbooks and are open. 尝试下面的代码:假设File1和File2是两个不同的工作簿并处于打开状态。

Sub Find_Barcode()

    Dim lastRow As Long, rngFind As Range, rngFound As Range
    lastRow = Workbooks("File2").Sheets("sheet1").Range("A65000").End(xlUp).Row

    Workbooks("File1").Activate
    Set rngFind = Workbooks("File1").Sheets("sheet2").Range("E5", Range("E65000").End(xlUp).Row)


    For i = 3 To lastRow

        Set rngFound = rngFind.Find(what:=Workbooks("File2").Sheets("sheet1").Cells(i, 1))
        If Not rngFound Is Nothing Then
            rngFound.Offset(0, -1).Copy Workbooks("File2").Sheets("sheet1").Cells(i, 4)
        End If

    Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM