ByRef参数类型不匹配VBA

时间:2017-10-31 12:51:58

标签: vba

我们正在模拟比赛,但我们陷入困境,我们不知道问题出在哪里。

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

1 个答案:

答案 0 :(得分:0)

在VB中,如果您没有指定,默认情况下您的参数将通过引用传递。您必须始终对所有参数使用ByVal,除非您确实希望通过引用传递某些参数。即便如此,为了清晰起见,我建议您使用ByRef

ByVal子例程中的每个参数名称前添加batasimulation,如下所示:

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)