简体   繁体   中英

Improper Output from call to subroutine

My program is meant to take input of GPA, student status, and credit hours from an excel worksheet and use them to calculate tuition, fees, discount, and total amount due for a given student.

I have to use separate subroutines to solve the tuition, fees, discount, and total for each person in the worksheet.

My problem is when I try to call a different subroutine to my primary subroutine and get a value I need, it displays a random number and doesn't use access any part of the fees or other subroutines set values.

I've tried moving my declarations, but the code just gets more errors.

'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

I need it to calculate a students tuition based off their credit hours and status in college.

In my code, I had it do an undergrad with 10 credit hours. This should result in a Tuition of $3,350.00 but instead it just turns out a value of $300.00.

I have no idea where it gets the 300 from.

It is hard to say without seeing the entire thing (data + macro), but I would bet on the fact that you don't have the Workbook or Worksheet declaration in your main Subroutine. Now it could read from some other sheet or even workbook that is open.

I'd add:

    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

End sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM