我的程序旨在从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美元。
我不知道它从哪里得到的。
答案 0 :(得分:0)
很难看到全部内容(数据+宏),但是我敢打赌,主子程序中没有Workbook或Worksheet声明。现在,它可以从其他打开的工作表甚至工作簿中读取。
我要添加:
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
结束子