简体   繁体   English

从调用到子程序的输出不正确

[英]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. 我的课程旨在从Excel工作表中输入GPA,学生身份和学分,并使用它们来计算给定学生的学费,学费,折扣和总金额。

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. 在我的代码中,我有一个10学分的本科生。 This should result in a Tuition of $3,350.00 but instead it just turns out a value of $300.00. 这应该导致3,350.00美元的学费,但它只是$ 300.00的价值。

I have no idea where it gets the 300 from. 我不知道从哪里获得300。

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 结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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