简体   繁体   中英

Copy cells and paste by overwriting cells in another sheet based on a criteria

I am trying to create a VBA code dependent on date. Here is my information:

Sheet 1: I have a table starting at cell A1 and going to G29. Cell B2 has the date, which has been coded using the TODAY option in Excel.

Now this table has to be updated every day, but once the day is over, I want to save the contents of this table and clear Sheet 1 for the next day. I want to save it all in a hidden sheet, Sheet 2.

At the moment, I have managed to get everything to copy and paste into Sheet 2 and then activate a cell below my table so it is ready to be copy and pasted the next day.

The issue I am having is that every time I run my macro, it copies and pastes the table into the selected cell below what I have already got in Sheet 2. So for any particular date, I could potentially have 3,4,5,whatever variations of the same table, all one underneath each other.

What I therefore want to do is tell my VBA code that if the value in cell B2 (sheet 1) is x and the value in the corresponding date cell in sheet 2 is the same, to copy and paste in those same cells.

I know how to do the last bit, what I would like help figuring out (for now) is how do I tell it to do my specific searches and activation of cells on both sheets, bearing in mind it needs to be kept general as after one date, the next date cell in Sheet 2 will be around 31 cells below B2, and then 31 cells below that, etc, etc.

Here is my current macro:

Sub Macro1()
'
' Macro1 Macro
'
' Acceso directo: CTRL+h
'
    Range("A1:G29").Select
    Selection.Copy
    Sheets("Hoja2").Select
    Range("A1").Select
    ActiveSheet.Paste
    Range("A1").Select
    ActiveCell.Offset(31, 0).Select

End Sub

This does my copy and paste and selects the cell below. However I am struggling with getting Excel to paste on a specific cell based on the date.

I hope this makes sense. If people have any questions then please let me know!

You want to check if the date in the source sheet is already present in the target one. If so, paste the 29-rows table in the corresponding range, else paste it at the bottom. The following should work:

Sub Macro1()

target = Cells("B2")
Range("A1:G29").Copy
end = Sheets("Hoja2").Cells(1048576, 2).End(xlUp).row 'check the last filled in row
tables = end / 29 'counts how many tables are already pasted in the sheet
for i = 1 to tables
    index = (i-1)*29 + 2 'row index where the date is written
    if Sheets("Hoja2").Cells("B" & index) = target then 'check if the date corresponds
        Sheets("Hoja2").Range("A" & index - 1).Paste
        Range("A" & index).Select
        ActiveCell.Offset(31, 0).Select
        exit sub
    end if
next i
'the following lines are skipped if the for loop pastes the table
Sheets("Hoja2").Range("A" & end + 1).Paste     
Range("A" & index).Select
ActiveCell.Offset(31, 0).Select

End Sub

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