使用vba从值另一个工作簿获取价值匹配的值?

我有以下的工作簿:

主手册

Column E Column G Column J Column V Expecting 12/12/2016 111 555 12/12/2015 222 444 12/12/2014 333 666 

我也有一个名为奴隶的工作簿:

从属工作簿

 Column C Column D Column E Column F Column G 111 555 12/12/2016 400 222 444 12/12/2015 500 333 666 12/12/2014 600 

如果master中每列中的值与从属工作簿中的值匹配,我想将来自slave的列G的值复制到master中的v列。

目前我的代码工作,如果我检查以下条件:

 Master Slave Column G MATCHES Column C Column J MATCHES Column D 

但是,如果我想检查在我的主工作簿列E中的date匹配从属工作簿列E我的代码停止工作。

这是我的代码:

 Option Explicit Option Compare Text Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim oCell As Range, oCell2 As Range, oCell3 As Range, oCell4 As Range, targetCell As Range Dim ws2 As Worksheet Dim lastRow As Long If Target.Column = 22 And ActiveCell.Value = "Expecting" Then If Not GetWb("Order Checker", ws2) Then Exit Sub lastRow = Range("J" & Rows.Count).End(xlUp).Row With ws2 For Each targetCell In Range("J6:J" & lastRow) Set oCell = .Range("D1", .Cells(.Rows.Count, "D").End(xlUp)).Find(what:=targetCell.Value, LookIn:=xlValues, lookat:=xlWhole) Set oCell2 = .Range("C1", .Cells(.Rows.Count, "C").End(xlUp)).Find(what:=targetCell.Offset(0, -3).Value, LookIn:=xlValues, lookat:=xlWhole) Set oCell3 = .Range("F1", .Cells(.Rows.Count, "F").End(xlUp)).Find(what:=targetCell.Offset(0, -5).Value, LookIn:=xlValues, lookat:=xlWhole) MsgBox oCell3 If Not oCell Is Nothing And Not oCell2 Is Nothing And Not oCell3 Is Nothing Then Application.EnableEvents = False targetCell.Offset(0, 12).Value = oCell.Offset(0, 3) Application.EnableEvents = True End If Next End With End If End Sub Function GetWb(wbNameLike As String, ws As Worksheet) As Boolean Dim wb As Workbook For Each wb In Workbooks If wb.Name Like "*" & wbNameLike & "*" Then '<-- check if workbook name contains "Depot Memo" Set ws = wb.Worksheets(2) Exit For End If Next GetWb = Not ws Is Nothing End Function 

问题是这样做的:

 Set oCell3 = .Range("E1", .Cells(.Rows.Count, "E").End(xlUp)).Find(what:=targetCell.Offset(0, -5).Value, LookIn:=xlValues, lookat:=xlWhole) 

由于某种原因,oCell3说它没有。

所以当我testing时,如果oCell3不是这样的:

 If Not oCell3 Is Nothing Then 

这导致我的代码不产生结果。

有人可以告诉我我做错了什么? 谢谢

问题可能是在ws2而searchdate是一个真正的 Date

在这种情况下,你可以使用:

 Set oCell3 = .Range("F1", .Cells(.Rows.Count, "F").End(xlUp)).Find(what:=CStr(targetCell.Offset(0, -5)), LookIn:=xlValues, lookat:=xlWhole)