简体   繁体   English

Excel的VBA代码-在另一个工作簿中查找一个单元格值,并将相邻的单元格复制到第一个工作簿中

[英]VBA codes for Excel- Find a cells value in another workbook and copy adjacent cell to the first workbook

I have a quick question; 我有一个快速的问题; I have two workbooks, let's say wbk1 and wbk2; 我有两个工作簿,分别是wbk1和wbk2; What I am trying to do is finding each cell value of a column in wbk1 in a specific column of wbk2 and return offset cell value from wbk2 to adjacent cell of the searched value in wbk1. 我想做的是在wbk2的特定列中找到wbk1中一列的每个像元值,并将偏移量像元值从wbk2返回到wbk1中搜索值的相邻像元。

Is there any one who can help? 有谁可以帮忙吗? By the way, I could find following code if it helps; 顺便说一下,如果有帮助,我可以找到以下代码; '================= “=================

Sub find1()

Dim Key
Dim Target
Dim Hnum
Dim Success
Dim wbk As Workbook
Dim Lastrow As Long
Dim one As Long

Success = False
Application.ScreenUpdating = False

strSecondFile = "C:\Soroush\08- Technical\03- Stock\Test\PL_Test_01\PL_Test_01.xlsm"
strFrthFile = "C:\Soroush\08- Technical\03- Stock\CNC_Test.xlsx"
'==

 Sheets("sheet2").Select

    Lastrow = ActiveSheet.UsedRange.Rows.Count

    If Not IsEmpty(Cells(5, 9).Value) Then
    Key = Cells(5, 9).Value

 For i = 5 To Lastrow

' If Not IsEmpty(Cells(i, 9).Value) Then
'    Key = Cells(i, 9).Value

 Set wbk = Workbooks.Open(strFrthFile)
 With wbk.Sheets("Sheet1")

    Set Target = Columns(1).find(Key, LookIn:=xlValues)

    If Not Target Is Nothing Then

        ActiveCell.Select
        Selection.Copy


        Set wbk = Workbooks.Open(strSecondFile)
            With wbk.Sheets("Sheet2")
                Sheets("Sheet2").Select
                    Cells(i, 10).Select
                        ActiveCell.Paste


                 If Not IsEmpty(Cells(i + 1, 9).Value) Then
                    Key = Cells(i + 1, 9).Value

            End If

        End With

    End If

 End With
'End If

 Next
    End If

End Sub


'=========================================================================

But It does not work and I can't figure it out. 但这行不通,我无法弄清楚。 I appreciate any comments on this macro. 感谢您对此宏的任何评论。

Cheers 干杯

You can use INDEX and MATCH. 您可以使用INDEX和MATCH。

在此处输入图片说明

In this case, the value in cell E2 is matched against values in B2 to B6. 在这种情况下,单元格E2中的值与B2至B6中的值匹配。 This is then indexed against column C and the value in column C is returned. 然后针对C列对此进行索引,并返回C列中的值。

*ignore the typo for "mammal"! *忽略“哺乳动物”的错字!

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

相关问题 如何使用“FIND”作为第一行和最后一行来查看值,匹配值并将相邻单元格复制到另一个工作簿? - How to use "FIND" as a first and last row to look value at, to match value and copy adjacent cell to another workbook? VBA Excel 复制单元格范围从另一个工作簿到活动工作簿 - VBA Excel copy cells range from another workbook to active workbook Excel VBA:将单元格值从一个工作簿复制到另一个? - excel vba: copy cell value from one workbook to another? Excel VBA:将特定工作簿中的单元格循环复制到另一个 - Excel VBA: Copy cells from specific workbook in loop to another Excel VBA宏,它将复制一系列单元格并粘贴到另一个工作簿中 - Excel VBA macro that will copy a range of cells and paste into another workbook Excel VBA:循环遍历单元格并将值复制到另一个工作簿 - Excel VBA: Loop through cells and copy values to another workbook 将所有 VBA 代码从工作簿复制到另一个 - Copy all VBA codes from workbook to another 搜索一个单词,然后将其旁边的单元格复制到另一个带有VBA的Excel工作簿中 - Search for a word then copy the cell next to it into another Excel workbook with VBA Excel VBA:从另一个工作簿循环中复制和粘贴特定单元格 - Excel VBA: Copy and Paste Specific Cell from Another Workbook Loop VBA Excel 将单元格范围复制到外部工作簿 - VBA Excel copy cell range to external workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM