计算、复制并粘贴到 VBA 中的给定值
Calculate, Copy and Paste to a given value in VBA
我是 VBA 的新手。我正在尝试根据编号自动进行迭代。单元格 "E2" 中指定的迭代次数。我希望 excel 从单元格 "A4" 自动填充 A 列到单元格 "E2" 的值,例如,如果 E2 = 100,Excel 将自动填充系列 1、2、3.. .下降到 100.
然后我希望 excel 连续计算单元格 "B2" 的值,然后将每个结果复制并粘贴到 B 列,从 "B4" 开始并在迭代值 "E2"
我有以下 "Autofill"
代码
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
最简单的方法是什么?
不错的 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
我是 VBA 的新手。我正在尝试根据编号自动进行迭代。单元格 "E2" 中指定的迭代次数。我希望 excel 从单元格 "A4" 自动填充 A 列到单元格 "E2" 的值,例如,如果 E2 = 100,Excel 将自动填充系列 1、2、3.. .下降到 100.
然后我希望 excel 连续计算单元格 "B2" 的值,然后将每个结果复制并粘贴到 B 列,从 "B4" 开始并在迭代值 "E2"
我有以下 "Autofill"
代码 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
最简单的方法是什么?
不错的 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