简体   繁体   中英

Excel/VBA - Copy and paste data into a worksheet in a specific row based on date

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.

I have two issues with this:

  1. 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".

  2. I also need it to be a paste special, with only the values being pasted.

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!

Assuming that your range will always be in B5:AC5, here is what I came up with:

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.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM