vba复制另一个工作簿中的相应值?

vba复制另一个工作簿中的相应值?

问题描述:

我有两个工作簿:vba复制另一个工作簿中的相应值?

规划

Column K  Column AG 
123    £100 
246    £20 
555    £80 

Column D  Column R 
123   £100 
246   £20 
555   £80 

我想要的值从计划复制,栏AG到R列(主)在我的项目号码D列(主)与K列(Planner)匹配。

我的下面的代码没有产生任何错误,并且没有产生任何结果 - 尽管它们有几个匹配。

请有人能告诉我我要去哪里吗?

为了避免疑惑,我的工作手册确实可以打开,所以查找文件。

代码

Sub PlannerOpen() 

'Set Variables 
Dim wb2 As Workbook 
Dim i As Long 
Dim j As Long 
Dim lastRow As Long 
Dim app As New Excel.Application  

'Find Planner 
If Len(FindDepotMemo) Then   
    'If Found Then Set Planner Reference. 
    app.Visible = False 'Visible is False by default, so this isn't necessary 
    Application.DisplayAlerts = False 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 
    Set wb2 = Workbooks.Open(FindDepotMemo, ReadOnly:=True, UpdateLinks:=False) 

    'If We have our planner lets continue... 

    'With my workbook 
    With wb2.Worksheets(1) 
     lastRow = .Cells(.Rows.Count, "K").End(xlUp).Row 

     'Lets begin our data merge 
     j = 2 
     For i = 2 To lastRow 
      'If data meets criteria 
      'Check Planner For Turnover 
      If ThisWorkbook.Worksheets("Data").Range("D" & j).Value = .Range("K" & i).Value Then ' check if Item number matches 
       ThisWorkbook.Worksheets("Data").Range("R" & j).Value = .Range("AG" & i).Value 

       j = j + 1 
      End If 
      'Continue until all results found 
     Next i 
    End With 

    'All Done, Let's tidy up 
    'Close Workbooks 
    'wb2.Close SaveChanges:=False 
    'app.Quit 
    'Set app = Nothing 

    Application.DisplayAlerts = True 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 
End If 

End Sub 

Function FindDepotMemo() As String 

    Dim Path As String 
    Dim FindFirstFile As String 

    Path = "G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\" & "8." & " " & Year(Date) & "\" 
    FindFirstFile = Dir$(Path & "*.xlsx") 
    While (FindFirstFile <> "") 
     If InStr(FindFirstFile, "Planner") > 0 Then 
      FindDepotMemo = Path & FindFirstFile 
      Exit Function 
     End If 
     FindFirstFile = Dir 
    Wend 

End Function 
+0

这看起来像在工作['VLOOKUP()']( https://support.office.com/en-gb/article/VLOOKUP-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1)功能。 – Phylogenesis

+0

@Phylogenesis如果可能,我想保留它vba – user7415328

+0

只有在i = 2和j = 2的情况下,您的代码才会查看并且递增,因此vba只检查1个值,然后转到下一个值。 ..但你想要的是1的价值检查范围内,如果发现返回价值......正确...你可能需要另一个循环获得 –

而不必2个For环路,只是使用Application.Match找到值之间的匹配在2个工作簿。

使用下面这段代码段与您的更换:

With wb2.Worksheets(1) 
     Dim MatchRow As Variant '<-- define variable to get the row number if Match is successful 

     lastRow = .Cells(.Rows.Count, "K").End(xlUp).Row 

     'Lets begin our data merge 
     For i = 2 To lastRow 
      ' If data meets criteria 
      ' Check Planner For Turnover 
      ' Use Application.Match to find matching results between workbooks 
      If Not IsError(Application.Match(ThisWorkbook.Worksheets("Data").Range("D" & i).Value, .Range("K2:K" & lastorw), 0)) Then ' check if Match is successful 
       MatchRow = Application.Match(ThisWorkbook.Worksheets("Data").Range("D" & i).Value, .Range("K2:K" & lastorw), 0) ' <-- get the row number where the match was found 
       ThisWorkbook.Worksheets("Data").Range("R" & j).Value = .Range("AG" & MatchRow).Value 
      End If 
      'Continue until all results found 
     Next i 
    End With 

你可以重构你的代码如下:

Option Explicit 

Sub PlannerOpen() 
    Dim dataRng As Range, cell As Range 
    Dim depotMemo As String 
    Dim iRow As Variant 

    If FindDepotMemo(depotMemo) Then '<--| if successfully found the wanted file   
     With ThisWorkbook.Worksheets("Data1") '<--| reference your "Master" workbook relevant worksheet 
      Set dataRng = .Range("D2", .Cells(.Rows.Count, "D").End(xlUp)) '<--| set its item numbers range 
     End With 

     With Workbooks.Open(depotMemo, ReadOnly:=True, UpdateLinks:=False).Worksheets(1) '<--| open depotMemo workbook and reference its first worksheet 
      For Each cell In .Range("K2", .Cells(.Rows.Count, "K").End(xlUp)) '<--| loop through referenced worksheet column "K" cells from row 2 down to last not empty one 
       iRow = Application.Match(cell.Value, dataRng, 0) '<--| try finding current depotMemo item number in Master item numbers range 
       If Not IsError(iRow) Then dataRng(iRow, 1).Offset(, 14).Value = cell.Offset(, 22) '<--| if found then grab depotMemo current item amount and place it in corresponding "master" data sheet column R 
      Next 
      .Parent.Close False 
     End With 
    End If  
End Sub 

Function FindDepotMemo(depotMemo As String) As Boolean  
    Dim Path As String 
    Dim FindFirstFile As String 

    Path = "G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\" & "8." & " " & Year(Date) & "\" 
    FindFirstFile = Dir$(Path & "*.xlsx") 
    While (FindFirstFile <> "") 
     If InStr(FindFirstFile, "Planner") > 0 Then 
      FindDepotMemo = True 
      depotMemo = Path & FindFirstFile 
      Exit Function 
     End If 
     FindFirstFile = Dir 
    Wend  
End Function 
+0

@ user7415328,你通过它吗? – user3598756

+0

工作完美。谢谢! – user7415328