需要一个循环,重新执行相同的公式,但在下一列

正如你所看到的,我想每次重复下一个值…有没有更简单的方法来做到这一点?

我已经试过循环function,但我不知道如何得到这个工作,以便它执行到下一列,每次为同一行。

Sub rebuild() If D28 > 2600000 Then Range("E$110:I$120").Select Selection.Copy Range("E18:Il28").Select ActiveSheet.Paste End If If E28 > 2600000 Then Range("E$110:I$120").Select Selection.Copy Range("F18:Jl28").Select ActiveSheet.Paste End If If F28 > 2600000 Then Range("E$110:I$120").Select Selection.Copy Range("G18:Kl28").Select ActiveSheet.Paste End If If G28 > 2600000 Then Range("E$110:I$120").Select Selection.Copy Range("H18:Ll28").Select ActiveSheet.Paste End If End sub 

这真的很简单。 我已经评论了代码。

你也不需要select一个范围来复制/粘贴。 你可能想看到这个

 Sub rebuild() With Sheet1 '~~> Change this to the relevant sheet For i = 4 To 6 '<~~ Col 4 (D) to Col 6 (F) If .Cells(28, i).Value > 2600000 Then '~~> Increment the range wgere you want to paste .Range("E$110:I$120").Copy .Range(.Cells(18, i + 1), .Cells(128, i + 5)) End If Next i End With End Sub 

一个解决scheme可能是把你的variables放到一个数组中,然后做一个简单的for循环。

 Dim MyArray(4, 2) as Variant MyArray(0,0) = D28 MyArray(0,1) = Range("E18:Il28") MyArray(1,0) = E28 MyArray(1,1) = Range("F18:Jl28") MyArray(2,0) = F28 MyArray(2,1) = Range("G18:Kl28") MyArray(3,0) = G28 MyArray(3,1) = Range("H18:Ll28") For i = LBound(MyArray) to UBound(MyArray) If MyArray(i,0) > 2600000 then Range("E$110:I$120").Copy MyArray(i,1).Paste End If Next