使用if函数复制/粘贴值

我正在尝试在VBA中编写一个代码,当主工作表是特定date时,该代码将从另一个工作表中将值复制到主工作表中。 我的数据是这样设置的:

在主工作表的A列中,我有我的date(mm / dd / yy)。 列B和C具有与计算无关的数据,但需要在那里。 列D到G是我要粘贴源工作表中的值的位置。 例如,如果A19是02/13/15,我希望macros将源工作表中的数据导入单元格D19:G19。

这是我目前使用的代码。 我将不胜感激任何帮助。 谢谢!

Sub newline() strdate = InputBox("Date") D = CStr(strdate) 's4 is the master worksheet Set s4 = Sheets(4) 's5 is the source worksheet that updates every day with different data Set s5 = Sheets(5) n = s4.Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count i = 0 For j = 1 To n If s4.Cells(j, 1).Value = D Then i = i + 4 'name of the cells in the source worksheet Set r = s5.Range("AccrualPayments") r.Copy s4.Cells(j, i).PasteSpecial (xlPasteValues) End If Next j End Sub 

你在哪里遇到错误? 还是有什么特别的这不是在做什么?

编辑:

 Sub newline() Dim strdate as String Dim s4, s5 as Worksheet Dim i, j, n as Integer strdate = InputBox("Date") 's4 is the master worksheet Set s4 = Sheets(4) 's5 is the source worksheet that updates every day with different data Set s5 = Sheets(5) n = s4.Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count i = 0 For j = 1 To n If s4.Cells(j, 1) = strdate Then i = i + 4 'name of the cells in the source worksheet s5.Range("AccrualPayments").copy s4.Cells(j, i).PasteSpecial Paste:=xlPasteValues End If Next j End Sub 

确保您input的date和您的主表上的date是相同的格式。 例如,2015年2月2日可以写成“2/2/2015”,“02/2/2015”或“02/02/2015”。 否则,看起来好像这个代码应该工作。

每个date只会在你的工作表“s4”中显示一次? 如果不是这样,第一个例子将在D列中粘贴范围“AccrualPayments”,H列中的第二个,第三个L等等。如果它们只出现一次,这将不是问题。