我对VBA很新。我试图基于no来自动化迭代。在单元格“E2”中指定的迭代次数。我希望excel将单元格“A4”中的A列自动填充到单元格“E2”的值,例如,如果E2 = 100,Excel将自动填充1,2,3系列...降至100。
然后我想要excel继续计算单元格“B2”的值然后将每个结果复制并粘贴到B列,从“B4”开始并停在迭代值“E2”
我有“自动填充”的以下代码
Sub Monte3()
Dim srcRange As Range
Dim destRange As Range
Range("A5:A1000000").ClearContents
Set srcRange = ActiveSheet.Range("A4")
Set destRange = ActiveSheet.Range("A4:A103")
srcRange.AutoFill destRange, xlFillSeries
End Sub
我已经录制了以下用于复制粘贴的宏
Sub Macro10()
Application.CutCopyMode = False
Calculate
Range("B2").Select
Selection.Copy
Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Calculate
Range("B2").Select
Selection.Copy
Range("B5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Calculate
Range("B2").Select
Selection.Copy
Range("B6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Calculate
Range("B2").Select
Selection.Copy
Range("B7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
最简单的方法是什么?
答案 0 :(得分:0)
一个不错的For Each Next Loop
应该有效。请参阅下面的代码。我根据你上面写的内容对一些范围参考进行了一些猜测,但你应该能够轻松地修改它以满足你的需要。
Sub Monte3()
Dim srcRange As Range, cel As Range
Dim wks As Worksheet
Set wks = Sheets("Sheet1") 'replace Sheet1 with your sheet name
With wks
.Range("B5:B1000000").ClearContents
Set srcRange = .Range("B4:B" & .Range("E2").Value + 4) 'will plug the number in from E2 as the row and adds 4 since you are starting at row 4
For Each cel In srcRange
With .Range("B2")
.Calculate
.Copy
End With
cel.PasteSpecial xlPasteValues
Next
End With
End Sub