【发布时间】:2015-02-14 04:18:37
【问题描述】:
我正在尝试在 VBA 中编写代码,只要主工作表是特定的一天,它就会将另一个工作表中的值复制到主工作表中。我的数据是这样设置的:
在主工作表的 A 列中,我有特定月份的日期 (mm/dd/yy)。 B 列和 C 列包含与计算无关但需要存在的数据。 D 到 G 列是我要粘贴源工作表中的值的位置。例如,如果 A19 是 2015 年 2 月 13 日,我希望宏将数据从源工作表中提取到单元格 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
【问题讨论】: