excel - 通过 VBA 代码生成的示例是重复的
问题描述
我已经编写了一个代码来生成样本,但是这些行在第一行中不断重复相同的数字。
我附上了一个带有示例和宏的 Excel 电子表格。
我当前的宏:
Sub PSA_Dist_SampleGenerator()
'Step 1: Assigning a variable to copy iterations for the loop function.
Dim Index As Integer 'Index is to ensure loop function runs up to a specified number of runs.
'Step 2: Altering application settings.
'Setting screen updating off for a fast and crash-proof process.
Application.ScreenUpdating = False
'Setting Excel calculations to 'manual' to decrease the time and processing requirements.
Application.Calculation = xlCalculationManual
'Step 3: Delete previously sampled/copied values
Sheets("covariance matrices (2)").Select
Range("Header").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
'Step 4: The loop that will copy the desired number of trials
Index = 1
Do While Index <= Range("Iteration_Number").Value
Application.StatusBar = "Running simulation number: " & Index & ", out of " &
Range("Iteration_Number").Value & " simulations."
Calculate
Range("Header").Offset(Index, 0).Value = Range("Sampled_Values").Value
Index = Index + 1
Loop
'Step 5: Resetting application settings.
Application.StatusBar = False
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
下面是一个替代宏,它也重复第一行(代码重复,因为试图压缩它导致没有生成样本)
Sub Generate_Samples_PSA()
'
Dim Iterations, n As Integer
Application.ScreenUpdating = False
Iterations = Range("w2").Value
For n = 1 To Iterations
Calculate
Range("K3:K4").Select
Selection.Copy
Range("S4").Select
ActiveCell.Offset(n, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Next n
For n = 1 To Iterations
Calculate
Range("L3:L4").Select
Selection.Copy
Range("U4").Select
ActiveCell.Offset(n, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Next n
For n = 1 To Iterations
Calculate
Range("M3:M4").Select
Selection.Copy
Range("W4").Select
ActiveCell.Offset(n, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Next n
For n = 1 To Iterations
Calculate
Range("N3:N4").Select
Selection.Copy
Range("Y4").Select
ActiveCell.Offset(n, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
Next n
End Sub
解决方案
参考第一个代码:该值重复,因为您正在设置所有值以Range("Sampled_Values").Value
尝试将行更改为
Range("Header").Offset(Index, 0).Value = Range("Sampled_Values").Offset(Index, 0).Value
推荐阅读
- python-3.x - 抽搐2.0。如何仅发送()给作者
- c - mbedtls 设置证书值...失败
- c# - web api中GET METHOD中的外键问题
- python - NameError:名称“FaceDetector”未定义
- android - 错误:升级 Android Studio 北极狐后打开模拟器 handleCpuAcceleration
- android - Android Rertifit:发布多个 2D 数组
- python - 需要在熊猫数据框中删除空列,dropna 没有帮助
- node.js - Express.js - 请求和响应未定义
- swift - 在 XCode 中使用方案管理文件
- c# - 使用来自 Amazon 的 SP-API C# 开发工具包构建说明