ByRef参数类型不匹配VBA
我们正在模拟一场比赛,但是我们被卡住了,我们不知道问题出在哪里 VBA通过变量ByRef参数类型不匹配VBA,vba,Vba,我们正在模拟一场比赛,但是我们被卡住了,我们不知道问题出在哪里 VBA通过变量BusPrice和在专用子模块下声明的其他变量向我们提供了一个错误。该代码来自多个模块 Sub batasimulation(NumberTeams As Integer, NumberStarts As Integer, _ TimeBetweenStarts As Integer, VanIntervalBefore As Integer, _ VanIntervalAfter As Integ
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
在VB中,如果不指定,默认情况下,参数将通过引用传递。必须始终对所有参数使用
ByVal
,除非确实希望通过引用传递某些参数。即使如此,为了清晰起见,我建议您使用ByRef
在batasimulation
子程序中的每个参数名称前面添加ByVal
,如下所示:
Sub registration(ByVal NumberTeams As Integer, ByVal CostsGeneral As Long, ByVal fee As Long, ByVal BreakfastPrice As Long, ByVal BreakfastPercentage As Long, ByVal DinnerPrice As Long, ByVal DinnerPercentage As Integer, ByVal BusPrice As Double, ByVal NumberTrajects As Integer, ByVal CostsBoardPersonal As Long, ByVal CostsTeam As Long, ByVal CostsRestart As Long)
在VB中,如果不指定,默认情况下,参数将通过引用传递。必须始终对所有参数使用
ByVal
,除非确实希望通过引用传递某些参数。即使如此,为了清晰起见,我建议您使用ByRef
在batasimulation
子程序中的每个参数名称前面添加ByVal
,如下所示:
Sub registration(ByVal NumberTeams As Integer, ByVal CostsGeneral As Long, ByVal fee As Long, ByVal BreakfastPrice As Long, ByVal BreakfastPercentage As Long, ByVal DinnerPrice As Long, ByVal DinnerPercentage As Integer, ByVal BusPrice As Double, ByVal NumberTrajects As Integer, ByVal CostsBoardPersonal As Long, ByVal CostsTeam As Long, ByVal CostsRestart As Long)
尝试在每个参数名称前面添加
ByVal
,如下所示:Sub batasimulation(ByVal numbertams为整数,ByVal…
。在第一个过程中,您没有声明变量,这可能是问题所在。您忘了将CostsGeneral
传递到注册()
,因此您的所有参数都被一个参数关闭,这自然会导致错误。请始终尝试将代码减少到产生错误所需的最小值,而不是发布完整的代码列表,请参见添加ByVal。Thnx以获取帮助!尝试在每个参数名称前面添加ByVal
,如下所示:Sub batasimulation(ByVal numbertams为整数,ByVal…
。在第一个过程中,您没有声明变量,这可能是问题所在。您忘记将成本一般
传递到注册()
,因此您的所有参数都会被一个参数关闭,这自然会导致错误。请不要发布完整的代码列表,而是始终尝试将代码减少到产生错误所需的最小值,有关帮助,请参阅添加ByVal worked.Thnx!