我的代码在下面工作,但我想添加一个函数。我有一个大型数据表,每行重复三次。在每组三个中,我增加了一个月两次。目的是将预测的销售额推迟到预计发货日期之后的一个月和两个月。现在,我想将E列中的值乘以因子到F列。每组三个中的原始行将在F列中=50%*E:E
,第二行在F列中将有=30%*E:E
,第三行在F列中将有=20%*E:E
。对于每一组三行,应该不断重复这个过程。问题:我当前的代码确实给了我正确的值,但是这些值比它们需要的低两个单元格。在此先感谢您的帮助!我目前的代码如下:
Public Sub DateAdd()
Dim r As Long
Dim l As Long
Dim Quant As Long
Dim dttTemp As Date
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("SalesForce Projects")
Application.ScreenUpdating = False
For r = ws.Range("A" & ws.Rows.Count).End(xlUp).Row To 1 Step -1
With ws.Cells(r, 1).EntireRow
.Copy
.Resize(2).Offset(1, 0).Insert Shift:=xlDown
End With
dttTemp = ws.Cells(r, "S").Value
ws.Cells(r + 1, "S").Value = DateSerial(Year(dttTemp), Month(dttTemp) + 1,
Day(dttTemp))
ws.Cells(r + 2, "S").Value = DateSerial(Year(dttTemp), Month(dttTemp) + 2,
Day(dttTemp))
Next r
Application.ScreenUpdating = True
'这是我的代码不好的地方
For l = ws.Range("A" & ws.Rows.Count).End(xlUp).Row To 1 Step -3
Quant = ws.Cells(l, "E").Value
ws.Cells(l, "F").Value = Cells(l, "E") * 0.5
ws.Cells(l + 1, "F").Value = Cells(l, "E") * 0.3
ws.Cells(l + 2, "F").Value = Cells(l, "E") * 0.2
Next l
End Sub
答案 0 :(得分:1)
为什么不在第一个循环中这样做?
Public Sub DateAdd()
Dim r As Long
Dim l As Long
Dim Quant As Long
Dim dttTemp As Date
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("SalesForce Projects")
Application.ScreenUpdating = False
For r = ws.Range("A" & ws.Rows.Count).End(xlUp).Row To 1 Step -1
With ws.Cells(r, 1).EntireRow
.Copy
.Resize(2).Offset(1, 0).Insert Shift:=xlDown
End With
dttTemp = ws.Cells(r, "S").Value
ws.Cells(r, "F").Value = Cells(r, "E") * 0.5 '\\ First line
ws.Cells(r + 1, "S").Value = DateSerial(Year(dttTemp), Month(dttTemp) + 1, Day(dttTemp))
ws.Cells(r + 1, "F").Value = Cells(r, "E") * 0.3 '\\ Second line
ws.Cells(r + 2, "S").Value = DateSerial(Year(dttTemp), Month(dttTemp) + 2, Day(dttTemp))
ws.Cells(r + 2, "F").Value = Cells(r, "E") * 0.2 '\\ Third line
Next r
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
当您说“E列中50%的值”时,您可能意味着E列中所有90的整个值,或者只是集合中三个单元格的值。如果你的意思是第一个(我假设第1行是标题,那么你的值从第2行开始。)在单元格F2中输入公式
=Sum(E:E)*.5
在单元格F3中输入
=SUM(E:E)*.3
在单元格F4中输入
=SUM(E:E)*.2
如果您的意思是另一个选项,请输入:
In F2 =Sum(E2:E4)*.5
in F3 =Sum(E2:E4)*.3
In F4 =Sum(E2:e4)*.2
现在选择f2:f4。将鼠标放在右下角,您会看到光标变为一个小的黑色十字。双击,公式将在工作表中复制。如果要填写多个工作表,则在开始此过程之前单击选项卡名称,然后将其复制到所有选定的工作表。
答案 2 :(得分:0)
在这种情况下,无需向后循环。将For
语句更新为以下内容:
For l = 1 to ws.Range("A" & ws.Rows.Count).End(xlUp).Row Step 3
然后它应该产生预期的结果。