首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ByRef参数类型失配

ByRef参数类型失配
EN

Stack Overflow用户
提问于 2017-10-31 12:51:58
回答 1查看 456关注 0票数 1

我们正在模拟一场比赛,但我们被困住了,我们不知道问题出在哪里。

VBA通过变量BusPrice和在私有子下面声明的其他变量给出一个错误。代码来自几个模块。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-31 17:44:27

在VB中,如果不指定,默认情况下您的参数将通过引用传递。您必须始终对所有参数使用ByVal,除非您确实希望通过引用传递一些参数。即使如此,我还是建议您使用ByRef来提高清晰度。

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47035923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档