该作业要求我运行蒙特卡罗结果1000次。我已经创建了一行30年的值(B5:AE5),我想重复这个过程1000次。每次都会出现一个新行,所有值都是随机的。
以下是我的代码,出于某种原因,它会转到我的Excel工作表的最底层。我想要第二排30年的价值(B6:AE6)。
Sub Macros()
Dim trail As Long
trail = InputBox("Enter the number of time you want to simulate this Macros", "Macros", "10")
For i = 1 To trail
Application.CutCopyMode = False
Range("B5").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Selection.End(xlDown).Select
Selection.Offset(-1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Range("A4").Select
Selection.End(xlDown).Select
Selection.Copy
Selection.Offset(1, 0).Select
ActiveSheet.Paste
Application.CutCopyMod = False
Next i
Range("B4").Select
End Sub
非常感谢你!
答案 0 :(得分:0)
要回答有关End(xlDown)
将您带到工作表末尾的原因的问题,Selection.End(xlDown).Select
类似于在电子表格上按Ctrl+Down
。 (同样Selection.End(xlToRight)).Select
类似于按Ctrl + Right。)
因此,如果您在空白工作表上,或者活动(或引用)单元格下方的所有单元格都为空,则按Ctrl+Down
会将您带到最后一行。
所有这一切,您可以通过
来避免整个问题并显着改善您的代码Select
语句并直接使用范围对象。 请参阅以下代码:
Sub Macros()
Dim trail As Long
trail = InputBox("Enter the number of time you want to simulate this Macros", "Macros", "10")
With Range(Range("B5"), Range("AE5"))
.Copy
.Offset(1).Resize(trail - 1, 30).PasteSpecial xlPasteValues
.Offset(1).Resize(trai1 - 1, 30).PasteSpecial xlPasteFormats
End With
With Range("A5")
.Copy .Offset(1).Resize(trail - 1)
End With
'if you don't need to copy the formats you can change the above With statements to just this:
'With Range("A5:BE5")
' .Offset(i).Resize(trail - 1,31).Value = .Value
'End With
End Sub
答案 1 :(得分:0)
听起来您想要将公式放在选定的行数中。
Sub Frmla()
Dim i As Long
i = InputBox("enter Number")
Range("B6:AE" & 5 + i).FormulaR1C1 = "=R[-1]C*0.7"'whatever the formula is
End Sub