我有2023年1月至12月的样本数据。从当前月份(七月)开始,我想将下三个月的值复制到新工作簿。
例如,对于当前月份6月,它将需要下三个月。如果是七月,那就是八月,九月,十月
销售工作簿-->新工作簿。
销售工作簿. xlsx
Month Value Desc
01-01-2023 300 001
01-02-2023 500 001
01-03-2023 200 001
01-04-2023 400 001
01-05-2023 500 001
01-06-2023 500 001
01-07-2023 700 001
01-08-2023 800 001
01-09-2023 200 001
01-10-2023 100 001
01-11-2023 300 001
01-12-2023 600 001
新建工作簿
01-07-2023 700 001
01-08-2023 800 001
01-09-2023 200 001
Sub CopyNextThreeMonths()
Dim sourceSheet As Worksheet
Dim destinationSheet As Worksheet
Dim currentDate As Date
Dim nextThreeMonths As Range
Dim nextMonth As Date
' Set the source sheet ( "Sales Workbook")
Set sourceSheet = ThisWorkbook.Sheets("Sales Workbook")
' Set the destination sheet ("New workbook")
Set destinationSheet = ThisWorkbook.Sheets("New workbook")
' Set the current date
currentDate = Date
' Set the range for the next three months
Set nextThreeMonths = destinationSheet.Range("A1:C100") ' Adjust the range as per your requirement
' Loop through each cell in the range and copy values from the corresponding cells in the source sheet
For Each cell In nextThreeMonths
nextMonth = DateAdd("m", 1, currentDate)
If Month(nextMonth) = Month(cell.Value) And Year(nextMonth) = Year(cell.Value) Then
cell.Value = sourceSheet.Range(cell.Address).Value
End If
Next cell
End Sub
1条答案
按热度按时间8ehkhllq1#
请尝试使用下一个代码:
它假设要处理的范围存在于列A:C中,从第一行开始。而且也将有数据后,12月,本年度时,代码将工作的10月-12月月...
如果没有单元格格式复制是必要的,代码可以很容易地适应复制数组的范围。
下一个版本是您的改编代码,使用您使用的工作表: