【发布时间】:2018-08-25 17:27:12
【问题描述】:
场景:这个问题与我之前的一个问题相关(答案有效,但这种另一种方法仍然让我很感兴趣,因为理论上它应该有效)。我正在尝试比较 2 个工作表中的数据。根据日期,我将一张表中的数据放入另一张表中。
说明:我的一张表有月度数据(EOM),而另一张有日度数据(工作日);两者都有相同的列项。只有日期与我的日常数组相关(其中的其他数据无关)。到目前为止,我有哪些代码,我将这些日期集放入数组中,然后循环遍历两者:获取第一个每月日期,然后检查第一个每日日期是否都 >= 到它,= 到 31/12/2010 并且
问题:由于某种原因,我似乎无法发现,代码运行,但粘贴数据不正确(例如,它将第一个每月日期的数据粘贴到所有列中的每日数据(包括 5 年以上)。
问题:知道是什么导致了这个问题吗?
代码:
Private Sub CommandButton2_Click()
Dim monthlydatesarray As Variant, monthlydataarray As Variant, dailydatesarray As Variant, dailydataarray As Variant
Dim xx As Long, monthlydaterow As Long, dailydaterow As Long, lastRowD As Long, lastRowM As Long
Dim wb As Workbook
Dim ws As Worksheet
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
lastRowD = Sheets("Bid").Cells.SpecialCells(xlCellTypeLastCell).Row
lastRowM = Sheets("AMT").Cells.SpecialCells(xlCellTypeLastCell).Row
For Each ws In wb.Worksheets
If ws.Name = "monthly" Then
dailydataarray = wb.Sheets("daily").UsedRange
dailydatesarray = wb.Sheets("daily").Range("A1:A" & lastRowD)
monthlydatesarray = wb.Sheets("monthly").Range("A1:A" & lastRowM)
monthlydataarray = wb.Sheets("monthly").UsedRange
'if date matches, use the data values
For monthlydaterow = 2 To UBound(monthlydatesarray)
For dailydaterow = 2 To UBound(dailydatesarray)
If dailydaterow = 2 And monthlydaterow = 2 Then
If dailydatesarray(dailydaterow, 1) >= monthlydatesarray(monthlydaterow, 1) And dailydatesarray(dailydaterow, 1) <= DateSerial(Year(monthlydatesarray(monthlydaterow, 1)), Month(monthlydatesarray(monthlydaterow, 1)) + 2, -1) Then
For xx = 2 To UBound(dailydataarray, 2)
dailydataarray(dailydaterow, xx) = monthlydataarray(monthlydaterow, xx)
Next xx
End If
Else
If dailydatesarray(dailydaterow, 1) >= monthlydatesarray(monthlydaterow, 1) And dailydatesarray(dailydaterow, 1) <= DateSerial(Year(monthlydatesarray(monthlydaterow, 1)), Month(monthlydatesarray(monthlydaterow, 1)) + 2, -1) Then
For xx = 2 To UBound(dailydataarray, 2)
dailydataarray(dailydaterow, xx) = monthlydataarray(monthlydaterow, xx)
Next xx
End If
End If
Next dailydaterow
Next monthlydaterow
'wb.Sheets("test").Clear
wb.Sheets("test").Range("A1").Resize(UBound(dailydataarray, 1), UBound(dailydataarray, 2)) = dailydataarray
ws.UsedRange.Columns(1).NumberFormat = "yyyy/mm/dd"
End If
Next ws
End Sub
数据示例:
Monthly date:
Item 1 Item 2
2012.05.31 0 499285000
2012.06.28 0 499285000
2012.07.31 400000000 296891000
2012.08.30 400000000 296891000
Daily date:
Item 1 Item 2
31.05.2012
01.06.2012
02.06.2012
03.06.2012
04.06.2012
05.06.2012
06.06.2012
07.06.2012
08.06.2012
09.06.2012
10.06.2012
11.06.2012
12.06.2012
13.06.2012
14.06.2012
15.06.2012
16.06.2012
17.06.2012
18.06.2012
19.06.2012
20.06.2012
21.06.2012
22.06.2012
23.06.2012
24.06.2012
25.06.2012
26.06.2012
27.06.2012
28.06.2012
29.06.2012
30.06.2012
01.07.2012
02.07.2012
03.07.2012
04.07.2012
05.07.2012
06.07.2012
07.07.2012
08.07.2012
09.07.2012
10.07.2012
11.07.2012
12.07.2012
13.07.2012
14.07.2012
15.07.2012
16.07.2012
17.07.2012
18.07.2012
19.07.2012
20.07.2012
21.07.2012
22.07.2012
23.07.2012
24.07.2012
25.07.2012
26.07.2012
27.07.2012
28.07.2012
29.07.2012
30.07.2012
31.07.2012
01.08.2012
02.08.2012
03.08.2012
04.08.2012
05.08.2012
06.08.2012
07.08.2012
08.08.2012
09.08.2012
10.08.2012
11.08.2012
12.08.2012
13.08.2012
14.08.2012
15.08.2012
16.08.2012
17.08.2012
18.08.2012
19.08.2012
20.08.2012
21.08.2012
22.08.2012
23.08.2012
24.08.2012
25.08.2012
26.08.2012
27.08.2012
28.08.2012
29.08.2012
Result arrays of the current code (relevant part):
Item 1 Item 2
31.05.2012 0 499285000
01.06.2012 0 499285000
02.06.2012 0 499285000
03.06.2012 0 499285000
04.06.2012 0 499285000
05.06.2012 0 499285000
06.06.2012 0 499285000
07.06.2012 0 499285000
08.06.2012 0 499285000
09.06.2012 0 499285000
10.06.2012 0 499285000
11.06.2012 0 499285000
12.06.2012 0 499285000
13.06.2012 0 499285000
14.06.2012 0 499285000
15.06.2012 0 499285000
16.06.2012 0 499285000
17.06.2012 0 499285000
18.06.2012 0 499285000
19.06.2012 0 499285000
20.06.2012 0 499285000
21.06.2012 0 499285000
22.06.2012 0 499285000
23.06.2012 0 499285000
24.06.2012 0 499285000
25.06.2012 0 499285000
26.06.2012 0 499285000
27.06.2012 0 499285000
28.06.2012 0 499285000
29.06.2012 0 499285000
30.06.2012 0 499285000
01.07.2012 0 499285000
02.07.2012 0 499285000
03.07.2012 0 499285000
04.07.2012 0 499285000
05.07.2012 0 499285000
06.07.2012 0 499285000
07.07.2012 0 499285000
08.07.2012 0 499285000
09.07.2012 0 499285000
10.07.2012 0 499285000
11.07.2012 0 499285000
12.07.2012 0 499285000
13.07.2012 0 499285000
14.07.2012 0 499285000
15.07.2012 0 499285000
16.07.2012 0 499285000
17.07.2012 0 499285000
18.07.2012 0 499285000
19.07.2012 0 499285000
20.07.2012 0 499285000
21.07.2012 0 499285000
22.07.2012 0 499285000
23.07.2012 0 499285000
24.07.2012 0 499285000
25.07.2012 0 499285000
26.07.2012 0 499285000
27.07.2012 0 499285000
28.07.2012 0 499285000
29.07.2012 0 499285000
30.07.2012 0 499285000
31.07.2012 0 499285000
01.08.2012 0 499285000
02.08.2012 0 499285000
03.08.2012 0 499285000
04.08.2012 0 499285000
05.08.2012 0 499285000
06.08.2012 0 499285000
07.08.2012 0 499285000
08.08.2012 0 499285000
09.08.2012 0 499285000
10.08.2012 0 499285000
11.08.2012 0 499285000
12.08.2012 0 499285000
13.08.2012 0 499285000
14.08.2012 0 499285000
15.08.2012 0 499285000
16.08.2012 0 499285000
17.08.2012 0 499285000
18.08.2012 0 499285000
19.08.2012 0 499285000
20.08.2012 0 499285000
21.08.2012 0 499285000
22.08.2012 0 499285000
23.08.2012 0 499285000
24.08.2012 0 499285000
25.08.2012 0 499285000
26.08.2012 0 499285000
27.08.2012 0 499285000
28.08.2012 0 499285000
29.08.2012 0 499285000
30.08.2012 0 499285000
【问题讨论】:
-
你能张贴一个截图来说明错误的结果吗?
-
@SJR 添加在问题的末尾。