简体   繁体   中英

Use value from column in one workbook to search column in another workbook

I'm having trouble with the code below.

I'm trying to use the values from column "A" in wb2 to search in column "G" in wb1.

Column "A" in wb2 contains a long list of numbers and I'm trying to search a exact match of that number in column "G" in wb1.

When there's a match I need it to set the value of column "AF" at the correct row in wb2 to the corresponding match from wb1, but from another column, maybe column "Z" instead of "G".

The to workbooks are already open, when running the macro.

Hope you can help me out.

Thanks in advance.

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

Here's how I would look to carry out your requirement (assuming I understood it clearly enough anyway!). This code loops through all rows in column A in wb2, and performs a Find operation against column G in wb1. Where it's found, it sets AF column in wb2 for that row to be the value from wb1's Z column on the same row.

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

This should fix your issue ( I've not wrote this in VBA so there might be the odd syntax issue).

Essentially, you can 'Find' your value in wb1 and if its there paste that value into 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
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

I might be little off on getting source data and target data.

It's really confusing

Anyway, You can play around with it to make it work :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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