将数据范围复制并粘贴到特定日期单元格

时间:2018-01-27 17:57:11

标签: excel vba

我有这个代码有效并且很棒但它只能找到我们所处的当前日期(因为DateValue(现在))我希望这可以在"输入&上设置一个单元格值#34;特别是工作表(单元格D4)。

任何人都知道怎么做?

Sub FindToday()

Dim FoundDate As Range
Set FoundDate = Worksheets("Data").Columns("A").Find(DateValue(Now), LookIn:=xlValues, lookat:=xlWhole)

If Not FoundDate Is Nothing Then ' if we don't find the date, simply skip.
    Worksheets("Input").Range("A4:H4").Copy
    FoundDate.Offset(0, 1).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False ' You can see that the first argument in PasteSpecial is set to only paste values.
End If
End Sub

全部谢谢

2 个答案:

答案 0 :(得分:0)

这里弹出一个输入框,这样您就可以选择从现在开始改变目标日期数天:例如,输入-3并将数据放入3天前...就像你在找什么?或者您是否希望它复制所有新数据?或者只是所有数据而不管日期?

Sub FindToday()
Dim N As Long
Dim FoundDate As Range
N = Application.InputBox("Enter Date Offset:", "Date Selector")
If N = False Then Exit Sub
Set FoundDate = Worksheets("Data").Columns("A").Find(DateValue(Now + N), LookIn:=xlValues, lookat:=xlWhole)

If Not FoundDate Is Nothing Then ' if we don't find the date, simply skip.
    Worksheets("Input").Range("A4:H4").Copy
    FoundDate.Offset(0, 1).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False ' You can see that the first argument in PasteSpecial is set to only paste values.
End If
End Sub

答案 1 :(得分:0)

只需使用范围:

Sub FindToday()
Dim searchDate as Range

Dim FoundDate As Range
Set searchDate = Worksheets("Input").Range("D4")
Set FoundDate = Worksheets("Data").Columns("A").Find(searchDate.Value, LookIn:=xlValues, lookat:=xlWhole)

If Not FoundDate Is Nothing Then ' if we don't find the date, simply skip.
    Worksheets("Input").Range("A4:H4").Copy
    FoundDate.Offset(0, 1).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False ' You can see that the first argument in PasteSpecial is set to only paste values.
End If
End Sub

或许你会做...Find(DateValue(searchDate.value)...