Excel/VBA - 根据日期将数据复制并粘贴到特定行的工作表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7013593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel/VBA - Copy and paste data into a worksheet in a specific row based on date
提问by Steven Ell
I have what I think is a pretty simple question.
我有一个我认为非常简单的问题。
I have a report that is updated daily. A specific range (B5:AC5) from one worksheet needs to be copied to a different worksheet on a daily basis. This doesn't need to be done automatically, I'd just like to add the code for it into a formatting macro I've created.
我有一份每天更新的报告。每天需要将一个工作表中的特定范围 (B5:AC5) 复制到不同的工作表中。这不需要自动完成,我只想将它的代码添加到我创建的格式化宏中。
I have two issues with this:
我有两个问题:
I want the data to be pasted in the row that corresponds with that specific day. Column A in worksheet "Daily" has the list of working days for that month. So all I need is for the macro to find today's date in Column A in "Daily", and paste b5:AC5 from "Matrix" in b?:ac? in that row on "Daily".
I also need it to be a paste special, with only the values being pasted.
我希望将数据粘贴到与该特定日期对应的行中。工作表“每日”中的 A 列包含该月的工作日列表。所以我所需要的只是让宏在“每日”的 A 列中找到今天的日期,然后将 b5:AC5 从“矩阵”粘贴到 b?:ac? 在“每日”的那一行。
我还需要它是一个特殊的粘贴,只粘贴值。
I'm very new to VB, but can usually follow code logic pretty well. Let me know if you need any more information from me. Thank you so much!
我对 VB 很陌生,但通常可以很好地遵循代码逻辑。如果您需要我提供更多信息,请告诉我。非常感谢!
采纳答案by Paul McLain
Assuming that your range will always be in B5:AC5, here is what I came up with:
假设您的范围始终在 B5:AC5 中,这是我想出的:
Sub FindToday()
Dim FoundDate As Range
Set FoundDate = Worksheets("Daily").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("Matrix").Range("B5:AC5").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
I tested this as best I could given your information. I put a set of ascending numbers in the B5:AC5 range, with formulas, put a set of ascending dates for one month in the Daily sheet, and it seems to do what you're looking for.
我尽我所能测试了这个,我可以给你的信息。我在 B5:AC5 范围内放置了一组升序数字,并带有公式,在每日工作表中放置了一组一个月的升序日期,它似乎可以满足您的需求。
Hope this helps.
希望这可以帮助。