[英]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.我正在尝试创建一个依赖于日期的 VBA 代码。 Here is my information:
这是我的信息:
Sheet 1: I have a table starting at cell A1 and going to G29.第 1 表:我有一个表格,从单元格 A1 开始并转到 G29。 Cell B2 has the date, which has been coded using the TODAY option in Excel.
单元格 B2 包含日期,该日期已使用 Excel 中的 TODAY 选项进行编码。
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.现在这张表必须每天更新,但是一旦一天结束,我想保存这张表的内容并清除第二天的表1。 I want to save it all in a hidden sheet, Sheet 2.
我想把它全部保存在一个隐藏的工作表中,工作表 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.目前,我已设法将所有内容复制并粘贴到工作表 2 中,然后激活表格下方的单元格,以便第二天复制和粘贴。
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.我遇到的问题是,每次运行宏时,它都会将表格复制并粘贴到我在工作表 2 中已经获得的单元格下方的选定单元格中。因此对于任何特定日期,我可能有 3、4、5,同一张桌子的任何变化,都在彼此之下。
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.因此,我想要做的是告诉我的 VBA 代码,如果单元格 B2(工作表 1)中的值是 x 并且工作表 2 中相应日期单元格中的值相同,则复制并粘贴到这些相同的单元格中。
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.我知道如何做最后一点,我想要帮助弄清楚(现在)是我如何告诉它在两张纸上进行特定搜索和激活单元格,请记住它需要保持一般性一个日期,工作表 2 中的下一个日期单元格将在 B2 下方约 31 个单元格处,然后在其下方 31 个单元格处,依此类推。
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.
但是,我正在努力让 Excel 根据日期粘贴到特定单元格上。
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.
如果是,则将 29 行的表格粘贴在相应的范围内,否则将其粘贴在底部。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.