Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-无法。在范围中找到日期字符串_Excel_Vba - Fatal编程技术网

Excel VBA-无法。在范围中找到日期字符串

Excel VBA-无法。在范围中找到日期字符串,excel,vba,Excel,Vba,我编写了一些VBA脚本来搜索列表中的日期范围,它可以找到日期,但由于某些原因,它无法将它们与目标范围匹配。我用一个vlookup测试了目标范围,它确实返回了一个匹配项,但是.find代码似乎没有以同样的方式工作 例如,sourcecolumnvalue将拾取其范围内的日期(例如2015年2月1日)。sourcecolumnvalue将反映这一点,但似乎无法在.find字符串中设置的目标范围内找到这一点 我的代码有问题吗 Sub Finddates() Dim SourceColumnValue

我编写了一些VBA脚本来搜索列表中的日期范围,它可以找到日期,但由于某些原因,它无法将它们与目标范围匹配。我用一个vlookup测试了目标范围,它确实返回了一个匹配项,但是.find代码似乎没有以同样的方式工作

例如,sourcecolumnvalue将拾取其范围内的日期(例如2015年2月1日)。sourcecolumnvalue将反映这一点,但似乎无法在.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

FIND
与日期一起使用是finnicky,请参见

当我更改时,您的代码在我的测试中起作用

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


我设法用循环编写了一些代码,而不是使用.find,它恰好与日期非常不一致。我在另一篇文章中读到,使用字符串表示日期更好,因为日期的实际数值存储在字符串中。我将源日期和目标日期转换为字符串,然后使用一个运行良好的循环进行匹配。但是谢谢你的回答,它确实让我走上了正确的道路

见下文

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

不,只是经验丰富:)谢谢你的快速收尾。它确实很有趣,我第二次运行它,它跳过了日期,第一次工作。我已重置excel并重新启动,将再次尝试。您是否可以发布一个示例文件供我们使用?
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