【发布时间】:2017-11-01 01:44:27
【问题描述】:
我们正在模拟一场比赛,但我们被卡住了,我们不知道问题出在哪里。
VBA 通过变量BusPrice 和在私有子下面声明的其他变量给我们一个错误。代码来自多个模块。
Sub batasimulation(NumberTeams As Integer, NumberStarts As Integer, _
TimeBetweenStarts As Integer, VanIntervalBefore As Integer, _
VanIntervalAfter As Integer, TimeWindow As Integer, _
CostsGeneral As Long, fee As Long, BreakfastPrice As Long, _
BreakfastPercentage As Long, DinnerPrice As Long, _
DinnerPercentage As Long, BusPrice As Double, _
NumberTrajects As Integer, CostsBoardPersonal As Long, _
CostsTeam As Long, CostsRestart As Long)
'
'this procedure simulates one bata race and determines the crowdedness at each node
'Define the worksheets
Dim LT, SP, ED, ST, KNP As Worksheet
Set LT = Sheets("SimRunningtimes")
Set SP = Sheets("StatTeams")
Set ED = Sheets("StageData")
Set ST = Sheets("SimStartTimes")
Set KNP = Sheets("SimNodes")
'aux variables
Dim stage As Integer 'counters
'disable updating the screen, speeds up code execution
Application.ScreenUpdating = False
LT.Cells.ClearContents
ST.Cells.ClearContents
LT.UsedRange 'dim sheet the size of used data, speeds up code
ST.UsedRange
'create headers in sheets SimRunningtimes and SimStartTimes
LT.Cells(2, 1).Value = "Teamtype"
ST.Cells(2, 1).Value = "Startgroup"
For stage = 1 To 25
LT.Cells(stage + 2, 1) = "stage " & stage
ST.Cells(stage + 2, 1) = "stage " & stage
Next stage
Call SimulateRunningTimes(NumberTeams) 'simulate running times(Q3)
Call DetermineStartTimes(NumberTeams, NumberStarts, TimeBetweenStarts) 'determine starttimes per stage (Q4)
Call nodes(TimeWindow, NumberTeams, VanIntervalBefore, VanIntervalAfter) ''determine crowdedness at nodes
Call registration(NumberTeams, fee, BreakfastPrice, BreakfastPercentage, DinnerPrice, DinnerPercentage, BusPrice, NumberTrajects, CostsBoardPersonal, CostsTeam, CostsGeneral, CostsRestart)
Application.ScreenUpdating = True
End Sub
Sub registration(NumberTeams As Integer, CostsGeneral As Long, fee As Long, BreakfastPrice As Long, BreakfastPercentage As Long, DinnerPrice As Long, DinnerPercentage As Integer, BusPrice As Double, NumberTrajects As Integer, CostsBoardPersonal As Long, CostsTeam As Long, CostsRestart As Long)
Dim BU As Worksheet
Dim i As Integer
Set BU = Sheets("budget")
BU.Cells(6, 10) = NumberTeams * CDbl(fee)
BU.Cells(20, 10) = NumberTeams * 24.34
BU.Cells(21, 10) = NumberTeams * CDbl(BusPrice) * CDbl(NumberTrajects)
BU.Cells(22, 10) = NumberTeams * CDbl(DinnerPrice) * CDbl(DinnerPercentage)
BU.Cells(23, 10) = NumberTeams * CDbl(BreakfastPrice) * CDbl(BreakfastPercentage)
BU.Cells(31, 10) = NumberTeams * 77.92
BU.Cells(32, 10) = NumberTeams * 92.3
BU.Cells(33, 10) = 43081 + (NumberStarts * 5000)
BU.Cells(38, 10) = NumberTeams * 97.39
BU.Cells(39, 10) = NumberTeams * 47.86
BU.Cells(40, 10) = NumberTeams * 22.02
BU.Cells(25, 10) = 0
For i = 6 To 23
BU.Cells(25, 10) = BU.Cells(25, 10) + BU.Cells(i, 10)
Next i
BU.Cells(43, 10) = 0
For i = 29 To 40
BU.Cells(43, 10) = BU.Cells(43, 10) + BU.Cells(i, 10)
Next i
BU.Cells(46, 10) = BU.Cells(25, 10) - BU.Cells(43, 10)
End Sub
Private Sub SimulateBtn_Click()
Dim NumberSimulations As Integer
Dim NumberTeams As Integer
Dim NumberStarts As Integer
Dim NumberRestarts As Integer
Dim TimeBetweenStarts As Integer
Dim TimeWindow As Integer
Dim VanIntervalBefore As Integer
Dim VanIntervalAfter As Integer
Dim s As Integer
Dim ED As Worksheet
Dim fee As Long
Dim BreakfastPrice As Long
Dim BreakfastPercentage As Long
Dim DinnerPrice As Long
Dim DinnerPercentage As Long
Dim BusPrice As Double
Dim NumberTrajects As Integer
Dim CostsBoardPersonal As Long
Dim CostsTeam As Long
Dim CostsGeneral As Long
Dim CostsRestarts As Long
For s = 1 To NumberSimulations
Call batasimulation(NumberTeams, NumberRestarts, TimeBetweenStarts, VanIntervalBefore, VanIntervalAfter, TimeWindow, CostsGeneral, fee, BreakfastPrice, BreakfastPercentage, DinnerPrice, DinnerPercentage, BusPrice, NumberTrajects, CostsBoardPersonal, CostsTeam, CostsRestarts)
Call CalculateKPI
Next s
【问题讨论】:
-
尝试在每个参数名称前添加
ByVal,如下所示:Sub batasimulation(ByVal NumberTeams As Integer, ByVal ...。 -
在第一个过程中,您没有声明变量,这可能是问题所在。
-
您忘记将
CostsGeneral传递给registration(),所以您的所有论点都偏离了一个,这自然会导致the error。 -
不要发布完整的代码清单,请始终尝试将代码减少到产生错误所需的最低限度,请参阅stackoverflow.com/help/mcve
-
添加 ByVal 有效。感谢您的帮助!
标签: vba