簡體   English   中英

從調用到子程序的輸出不正確

[英]Improper Output from call to subroutine

我的課程旨在從Excel工作表中輸入GPA,學生身份和學分,並使用它們來計算給定學生的學費,學費,折扣和總金額。

我必須使用單獨的子程序來解決工作表中每個人的學費,費用,折扣和總數。

我的問題是當我嘗試將一個不同的子程序調用到我的主子程序並獲得我需要的值時,它會顯示一個隨機數,並且不會使用訪問費用的任何部分或其他子程序設置值。

我已經嘗試移動我的聲明,但代碼只會出現更多錯誤。

'Primary subroutine
Sub Proj_5p2()

'Variables
Dim dblGPA As Double
Dim strStat As String 
Dim intRow As Integer
Dim intCredHrs As Integer
Dim curTuition As Currency
Dim curFees As Currency
Dim curDisc As Currency
Dim curTotal As Currency

'Processing
Do While (Range("a" & intRow) <> "")
    'Get required input for each row
    dblGPA = Range("c" & intRow).Value
    strStat = UCase(Range("d" & intRow).Value)
    intCredHrs = Range("e" & intRow).Value

    'Call subroutines
    Call Tuition(curTuition, intCredHrs, strStat)

    'Display subroutines
    Range("f" & intRow) = curTuition
Loop

End sub

'Call from subroutine
Sub Tuition(curTuition As Currency, intCredHrs As Integer, strStat As String)

   If strStat = "GRADUATE" Then
        If intCredHrs < 18 Then
            curTuition = 335 * intCredHrs
        Else
            curTuition = 6500
    End If

    ElseIf strStat = "UNDERGRADUATE" Then
        curTuition = 550 * intCredHrs
    End If

End Sub

我需要它根據他們的學分和大學狀態計算學生的學費。

在我的代碼中,我有一個10學分的本科生。 這應該導致3,350.00美元的學費,但它只是$ 300.00的價值。

我不知道從哪里獲得300。

沒有看到整個事物(數據+宏)很難說,但我敢打賭你的主子程序中沒有工作簿或工作表聲明。 現在它可以從其他一些工作表或甚至是開放的工作簿中讀取。

我補充說:

    Sub Proj_5p2()

    ' Delcare your workbook and worksheet
    Dim wb as Workbook
    Dim ws as Worksheet
    ' If data in the same workbook as the macro,
    ' or a static name: Set wb = Workbooks("bookname.xlsx")
    Set wb = ThisWorkbook 
    Set ws = wb.Worksheets("nameofsheet")


    'Variables
    Dim dblGPA As Double
    Dim strStat As String 
    Dim intRow As Integer
    Dim intCredHrs As Integer
    Dim curTuition As Currency
    Dim curFees As Currency
    Dim curDisc As Currency
    Dim curTotal As Currency

    ' Adds the worksheet reference, now each range object with .Range
    With ws

    'Processing
    Do While (.Range("a" & intRow) <> "")
    'Get required input for each row
    dblGPA = .Range("c" & intRow).Value
    strStat = UCase(.Range("d" & intRow).Value)
    intCredHrs = .Range("e" & intRow).Value

    'Call subroutines
    Call Tuition(curTuition, intCredHrs, strStat)

    'Display subroutines
    .Range("f" & intRow) = curTuition
    Loop

    End With

結束子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM