使用一个工作簿中的列的值来搜索另一个工作簿中的列

时间:2016-06-08 09:53:18

标签: excel vba excel-vba

我在使用下面的代码时遇到了问题。

我正在尝试使用wb2中“A”列的值来搜索wb1中的“G”列。

wb2中的列“A”包含一长串数字,我正在尝试在wb1中的“G”列中搜索该数字的完全匹配。

当匹配时,我需要它将wb2中正确行的列“AF”的值设置为wb1的相应匹配,但是从另一列设置,可能是“Z”列而不是“G”。

运行宏时,工作簿已经打开。

希望你能帮助我。

提前致谢。

Sub ROAC()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim y As Integer
Dim sht As Worksheet


Set wb1 = Workbooks("EP_BB_DK_ny.xlsm")
Set wb2 = Workbooks("Laaneoversigt.xlsm")
Set sht = wb2.Worksheets("oversigt")

LastRow = sht.Cells(sht.Rows.Count, "AF").End(xlUp).Row
LastRowWb1 = wb1.Sheets("Period").Range(wb1.Sheets("Period").Range("G1"), wb1.Sheets("Period").Range("G1").End(xlDown)).Rows.Count
LastRowWb2 = wb2.Sheets("Oversigt").Range(wb2.Sheets("Oversigt").Range("A1"), wb2.Sheets("Oversigt").Range("A1").End(xlDown)).Rows.Count

For y = 7 To LastRowWb1
For x = 1 To LastRowWb2


If wb1.Sheets("Period").Range("G" & y).Value = wb2.Sheets("Oversigt").Range("A" & x).Value Then

wb2.Sheets("Oversigt").Range("AF" & LastRow).Offset(1, 0).Value = wb1.Sheets("Period").Range("G" & y)

End If

Next x
Next y

End Sub

3 个答案:

答案 0 :(得分:0)

以下是我将如何执行您的要求(假设我无论如何都清楚地理解它!)。此代码循环遍历wb2中列A中的所有行,并对wb1中的列G执行查找操作。在找到它的地方,它将wb2中的AF列设置为同一行中wb1的Z列的值。

Sub ROAC()

Dim wb1 As Workbook
Dim wb2 As Workbook
Dim y As Integer
Dim sht As Worksheet

Set wb1 = Workbooks("EP_BB_DK_ny.xlsm")
Set wb2 = Workbooks("Laaneoversigt.xlsm")
Set wb1sht = wb1.Worksheets("Period")
Set wb2sht = wb2.Worksheets("oversigt")

LastRowWb1 = wb1sht.Cells(wb1sht.Rows.Count, "G").End(xlUp).Row
LastRowWb2 = wb2sht.Cells(wb2sht.Rows.Count, "A").End(xlUp).Row

For y = 1 To LastRowWb2
    findMe = wb2sht.Range("A" & y).Value
    With wb1sht.Range("G7:G" & LastRowWb1)
        Set oFound = .Find(findMe)
        If Not oFound Is Nothing Then
            ' Found number - set AF in wb2 on this row to Z on the same row from wb1
            wb2sht.Range("AF" & oFound.Row).Value = wb1sht.Range("Z" & oFound.Row).Value
        Else
            ' Didn't find number, so do whatever you might need to do to handle this in here...
        End If
    End With
Next

End Sub

答案 1 :(得分:0)

这应该可以解决您的问题(我没有在VBA中写这个,所以可能会出现奇怪的语法问题。)

基本上,你可以找到'你在wb1中的值,如果它在那里将该值粘贴到wb2。

Sub ROAC()
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim y As Integer
Dim sht As Worksheet
Dim fndRange as Range
Dim wb1Value as variant       

Set wb1 = Workbooks("EP_BB_DK_ny.xlsm")
Set wb2 = Workbooks("Laaneoversigt.xlsm")
Set sht = wb2.Worksheets("oversigt")

LastRow = sht.Cells(sht.Rows.Count, "AF").End(xlUp).Row
LastRowWb1 = wb2.Sheets("Period").Range("G" & Rows.Count).End(xlUp).Row
LastRowWb2 = wb2.Sheets("Oversigt").Range("A" & Rows.Count).End(xlUp).Row

For y = 7 To LastRowWb1

    wb1Value = wb1.Sheets("Period").Range("G" & y).Value

    Set fndRange = wb2.Sheets("Oversigt").Range("A1:A" & LastRowWb2).Find(What:= wb1Value)

    If Not fndRange is Nothing Then
        wb2.Sheets("Oversigt").Range("AF" & LastRow).Offset(1, 0).Value = wb1.Sheets("Period").Range("G" & fndRange.Row)
    End If

Next y

End Sub

答案 2 :(得分:0)

Sub ROAC()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim Target_Data As Range
Dim y As Integer

'Since you were using same sheets over and over, I just set ws1 and ws2 
'instead of writing Wb1.Sheets("Period") wb2.Sheets("Oversigt") everytime
Set ws1 = Workbooks("EP_BB_DK_ny.xlsm").SHEETS("Period")
Set ws2 = Workbooks("Laaneoversigt.xlsm").SHEETS("Oversigt")

lastrow = ws2.Cells(ws2.Rows.Count, "AF").End(xlUp).Row
LastRowWb1 = ws1.Range(ws1.Range("G1"), ws1.Range("G1").End(xlDown)).Rows.Count

For y = 7 To LastRowWb1

''''This compares ws1.Range("G" & y) with ws2.Column A and set Target_Data as matching range 
    Set Target_Data = ws2.Columns("A").Find(ws1.Range("G" & y).Value)

''''This check if the Target_data found data or not (Find method will return Nothing if it doesn't find it.)
    If Not (Target_Data Is Nothing) Then

''''''''This will write ws1. Column Z's data to ws2. Column AF on same row as where the data is found
        ws2.Range("AF" & Target_Data.Row) = ws1.Range("Z" & y)

    End If
Next y

End Sub

关于获取源数据和目标数据,我可能没什么用。

这真令人困惑

无论如何,你可以玩它来使它工作:)