Excel VBA – 不能查找范围内的datestring

我写了一些VBA脚本来search列表中的date范围,它可以finddate,但由于某种原因,它不能将它们匹配到目标范围。 我用一个确实返回一个匹配的vlookuptesting了目标范围,但是.find代码似乎不能以相同的方式工作。

例如,sourcecolumnvalue会在其范围内选取一个date(例如01/02/2015)。 sourcecolumnvalue会反映这个,但似乎无法在.findstring中设置的目标范围内find它。

我在做这个代码错了吗?

Sub Finddates() Dim SourceColumnValue As String, sourcerow As String, targetrow As String Dim M As Long, O As Long, TargetValue As Long, actualsourcerow As Long, actualtargetrow As Long, actualtargetcolumn As Long, sourcedateposition As Long TargetValue = dumpsheet.Cells(rows.Count, 1).End(xlUp).row sourcedateposition = dumpsheet.Cells(rows.Count, 5).End(xlUp).row 'Loop Source Column For F = 1 To sourcedateposition SourceColumnValue = dumpsheet.Cells(F, 5).Value 'Get Target Column Match to Source Set TargetColumnRange = dumpsheet.Range("G2:G" & TargetValue).Find(What:=SourceColumnValue, _ LookIn:=xlValues, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows) 'if a match is found If Not TargetColumnRange Is Nothing Then TargetColumnRange.Value = SourceColumnValue For O = 1 To dumpsheet.Range("A2:A" & rows.Count).End(xlUp).row Sourcename = ActiveCell(O, 1).Value sourcerow = ActiveCell(O, 2).Value targetrow = ActiveCell(O, 3).Value actualsourcerow = CInt(sourcerow) actualtargetrow = CInt(targetrow) actualtargetcolumn = CInt(TargetColumn) CapexTargetSheet.Activate Cells(actualtargetrow, actualtargetcolumn).Value = CapexSourceSheet.Cells(actualsourcerow, F).Value Next O End If Next F End Sub 

使用FINDdate是finnicky,看到这里

当我改变了你的代码在我的testing工作

 Set TargetColumnRange = dumpsheet.Range("G2:G" & TargetValue).Find(what:=SourceColumnValue, _ LookIn:=xlFormulas, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows) 

 Set TargetColumnRange = dumpsheet.Range("G2:G" & TargetValue).Find(what:=DATEVALUE(SourceColumnValue), _ LookIn:=xlFormulas, _ LookAt:=xlWhole, _ SearchOrder:=xlByRows) 

我设法使用一个循环来编写一些代码,而不是使用.find,而这恰好与date非常不一致。 我在另一篇文章中读到,使用datestring更好,因为date的实际数值存储在string中。 我将源和目标date转换为string,然后使用循环运行良好的匹配。 但是,谢谢你的回答,它确实把我放在正确的轨道上!

见下文

 Dim SourceColumnValue As String, sourcerow As String, targetrow As String, targetcolumnvalue As String, sourcecolumnnumber As String Dim M As Long, O As Long, P As Long, TargetValue As Long, actualsourcerow As Long, actualtargetrow As Long, actualtargetcolumn As Long, sourcedateposition As Long, actualsourcecolumn As Long, targetdateposition As Long Dim Copysource As Range, pastetarget As Range TargetValue = dumpsheet.Cells(rows.Count, 1).End(xlUp).row sourcedateposition = dumpsheet.Cells(rows.Count, 5).End(xlUp).row targetdateposition = dumpsheet.Cells(rows.Count, 7).End(xlUp).row 'Loop Source Column For F = 1 To sourcedateposition SourceColumnValue = dumpsheet.Cells(F, 5).Value 'Get Target Column Match to Source ' Loop to compare strings For P = 1 To targetdateposition targetcolumnvalue = dumpsheet.Cells(P, 7).Value If targetcolumnvalue = SourceColumnValue Then TargetColumnRange.Value = SourceColumnValue targetcolumnvalue = dumpsheet.Cells(P, 8).Value sourcecolumnnumber = dumpsheet.Cells(F, 6).Value For O = 1 To dumpsheet.Cells(rows.Count, "a").End(xlUp).row If O > 1 Then Sourcename = dumpsheet.Cells(O, 1).Value sourcerow = dumpsheet.Cells(O, 2).Value targetrow = dumpsheet.Cells(O, 3).Value 'Set Integers actualsourcerow = CInt(sourcerow) actualtargetrow = CInt(targetrow) actualtargetcolumn = CInt(targetcolumnvalue) actualsourcecolumn = CInt(sourcecolumnnumber) 'Copy and Paste Set Copysource = SourceSheet.Cells(actualsourcerow, actualsourcecolumn) Set pastetarget = TargetSheet.Cells(actualtargetrow, actualtargetcolumn) Copysource.Copy pastetarget.PasteSpecial (xlPasteValues) End If Next O End If Next P Next F