【发布时间】:2019-05-31 22:12:50
【问题描述】:
我正在尝试根据匹配的日期将数据从一张表复制到另一张表,到目前为止我可以做到这一点,但问题是相应日期的最新数据会覆盖同一日期的所有其他数据。
例如
我想从sheet 2 column 1 复制数据(基于column 2 中的日期)
我想将此数据粘贴到sheet 1 column 2(基于column 1 中的日期)
可以看出,只有sheet 2 column 1 中对应于相应日期的最后一个数字被粘贴到sheet 1 column 2 中的所有对应日期中。
相反,如果有两个日期,我希望将两个不同的数字(来自 sheet 2 column 1 )粘贴到 sheet 1 column 2 中。
我的原代码如下:
Sub Macroturnip()
'
' Macroturnip Macro
'
Dim Row As Double 'row is the row variable for the destination spreadsheet
Dim i As Date
Dim x As Long 'x is the row variable for the source spreadsheet
For Row = 1 To 825
i = Sheets("1").Cells(Row, 1)
If i <> DateSerial(1900, 1, 0) Then
'DateSerial(1900, 1, 0) --> this is the default for no data in the field, i.e. i want to skip these
For x = 2 To 450
If Sheets("2").Cells(x, 2) = Sheets("1").Cells(Row, 1) Then
Sheets("2").Select
Cells(x, 1).Select
Selection.Copy
Sheets("1").Select
Cells(Row, 2).Select
ActiveSheet.Paste
End If
Next x
End If
Next Row
End Sub
【问题讨论】:
-
表 1:(运行宏后):i.stack.imgur.com/hD50y.jpg
-
如果理解正确,如果您有两次特定日期,您希望将第二个值分配给第二个日期?这有点棘手,除非您在这些日期之间有 1:1 的关系......(即:工作表 1 有 5 个相同的日期,工作表 2 也有 5 个相同的日期)。