2017-10-31 2 views
1

우리는 경주를 시뮬레이션하고 있지만 우리는 붙어있어 문제가 어디에 있는지 알지 못합니다.ByRef 인수 유형 불일치 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 
+0

시도는이 같은 각 매개 변수의 이름 앞에'ByVal'를 추가 : 정수로 ByVal의 NumberTeams ('하위 batasimulation, ByVal ...'. –

+0

첫 번째 절차에서는 변수를 선언하지 않고 문제가 될 수 있습니다 –

+0

'CostsGeneral'을'registration()'에 전달하는 것을 잊어 버렸기 때문에 모든 인수는 자연스럽게 [오류] (https://stackoverflow.com/q/16611547/11683)가 발생합니다. – GSerg

답변

0

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)