以下问题:我想根据日期单元格(位于第一行)更新报表及其公式。循环应该持续到昨天的日期。这就是表格的样子:
如何实现动态添加日期,现在我想更新下面相应的公式(第2 - 35行),直到最新的日期条目。这是我到目前为止编写的代码:
Select
当我尝试复制活动的选定范围时,公式停止工作,并收到以下错误消息:
编译错误:未定义子或函数。
它指出了Sub Update_Newest_Day_Conversions()
Worksheets("CPC - Conversions DoD").Range("A1"). _
End(xlToRight).Select
MyDate = Date - 1
While ActiveCell.Value < MyDate
ActiveCell.Copy ActiveCell.Offset(0, 1)
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = ActiveCell.Value + 1
ActiveCell.Offset(1, -1).Select
Range(ActiveCell, ActiveCell.End(xlDown)).Select
ActiveRange.Copy Offset(0, 1)
Wend
End Sub
方法。
知道我做错了什么吗?我知道我非常接近。
答案 0 :(得分:1)
请尝试此代码。我对其进行了修改,以便更准确地描述您的要求。
Sub Update_Newest_Day_Conversions()
' 19 Feb 2018
Dim MyDate As Date
Dim LastDate As Date
Dim Rng As Range
Dim Rl As Long ' last row
Dim C As Long ' column counter
MyDate = Date - 1
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With Worksheets("CPC - Conversions DoD")
C = .Cells(1, .Columns.Count).End(xlToLeft).Column
Rl = .Cells(.Rows.Count, C).End(xlUp).Row
LastDate = .Cells(1, C).Value
Do While LastDate < MyDate
Set Rng = Range(.Cells(2, C), .Cells(Rl, C))
Rng.Copy Rng.Offset(0, 1)
LastDate = LastDate + 1
C = C + 1
.Cells(1, C).Value = LastDate
.Columns(C).AutoFit
Loop
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End Sub
答案 1 :(得分:0)
找到了解决方案以防任何人感兴趣:
Sub Update_Newest_Day_Conversions()
Worksheets("CPC - Conversions DoD").Range("A1"). _
End(xlToRight).Select
MyDate = Date - 1
While ActiveCell.Value < MyDate
ActiveCell.Copy ActiveCell.Offset(0, 1)
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = ActiveCell.Value + 1
ActiveCell.Offset(1, -1).Select
Range(ActiveCell, ActiveCell.End(xlDown)).Copy ActiveCell.Offset(0, 1)
ActiveCell.Offset(-1, 1).Select
Wend
End Sub