我有以下工作簿:
主要工作簿
Column E Column G Column J Column V
Expecting
12/12/2016 111 555
12/12/2015 222 444
12/12/2014 333 666
我还有一个名为slave的工作簿:
奴隶工作簿
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中的值从slave复制到master中的column v。
如果我检查以下标准,目前我的代码可以运行:
Master Slave
Column G MATCHES Column C
Column J MATCHES Column D
但是,如果我想检查主工作簿中E列的日期与从属工作簿上的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说它=没什么。
所以当我测试oCell3是不是这样的时候:
If Not oCell3 Is Nothing Then
这导致我的代码无法生成结果。
有人可以告诉我我做错了什么吗?感谢
答案 0 :(得分:0)
问题可能是,ws2
日期实际上是String
值,而搜索日期是真实 Date
。
在这种情况下你可以使用:
Set oCell3 = .Range("F1", .Cells(.Rows.Count, "F").End(xlUp)).Find(what:=CStr(targetCell.Offset(0, -5)), LookIn:=xlValues, lookat:=xlWhole)