VBA Excel将Main Sub中的值链接到其调用函数

时间:2013-03-14 07:33:05

标签: excel function excel-vba vba

我仍然非常擅长使用excel中的vba,并且在解决这个问题时遇到了困难。我真的希望有人能够告诉我我的代码出错的地方并尽可能简化我的解决方案。非常感谢&非常感谢!

问题: 贷款申请根据客户的收入进行评估。 我们应该使用For-Next函数和IF语句来显示以下批准状态:

客户的收入状况

  

60,000批准特价; > 40,000批准标准费率; > 24,000等待经理的批准;其他案例拒绝申请

从excel的第2行开始,B列包含“客户收入”数字,Excel中的C列包含“贷款申请状态”数字。

我的代码效果如下:

Sub ForNext_If()

'CINCOME = Customer Income,Loan status= the application status, x = the row number, FinalRow= last row number 

x = 2
For x = 2 To 11 Step 1

CINCOME = Cells(x, 2).Value

If CINCOME > 60000 Then
LOANSTATUS = "Approve with special rates"

ElseIf CINCOME > 40000 Then
LOANSTATUS = "Approve with standard rates"

ElseIf CINCOME > 24000 Then
LOANSTATUS = "Await manager's approval"

Else
LOANSTATUS = "Reject application"

End If

Cells(x, 4) = LOANSTATUS

Next

End Sub

但是,当我尝试使用调用函数时,

Sub ForNext_If()

x = 2
For x = 2 To 11 Step 1

CINCOME = Cells(x, 2).Value

Call Calling

Cells(x, 4) = LOANSTATUS

Next

End Sub

Function Calling()

If CINCOME > 60000 Then
LOANSTATUS = "Approve with special rates"

ElseIf CINCOME > 40000 Then
LOANSTATUS = "Approve with standard rates"

ElseIf CINCOME > 24000 Then
LOANSTATUS = "Await manager's approval"

Else
LOANSTATUS = "Reject application"

End If

End Function

CINCOME的价值似乎无法转移到“通话”功能,以确定贷款申请状态.T_T究竟出了什么问题?

1 个答案:

答案 0 :(得分:0)

继续评论。我为你准备了两个解决方案(还有更多,但这两个是第一个出现在我脑海中)。


  1. 用户定义的功能
    一旦将功能粘贴到任何模块中,您可以通过单击单元格并写入= LoanAppStatus(范围),即= LoanAppStatus(B2)+来使用工作表,然后将其拖到列中

    ' User Defined Function
    ' Returns the status of customers loan application based on the income
    Function LoanAppStatus(CINCOME As Range) As String
        If CINCOME.Value >= 24000 And CINCOME.Value < 40000 Then
            LoanAppStatus = "Await manager's approval"
    
    ElseIf CINCOME.Value >= 40000 And CINCOME.Value < 60000 Then
        LoanAppStatus = "Approve with standard rates"
    
    ElseIf CINCOME.Value > 60000 Then
        LoanAppStatus = "Approve with special rates"
    
    Else
        LoanAppStatus = "Reject application"
    End If
    

    结束职能


  2. 2.重写原来的子程序

    'Procedure
    ' Fills the status of customers loan application (Column C) based on the income ( Column B )
        Sub ForNext_If()
            Dim i As Long
            Dim wS As Worksheet
            Set wS = ThisWorkbook.ActiveSheet
    
            ' Range("B" & i).Value its what you called CINCOME
            ' Range("C" & i).Value its what you called LOANSTATUS
    
            With wS
              For i = 2 To Range("B" & Rows.Count).End(xlUp).Row
                If Range("B" & i).Value > 60000 Then
                   Range("C" & i).Value = "Approve with special rates"
                ElseIf Range("B" & i).Value >= 40000 And Range("B" & i).Value < 60000 Then
                   Range("C" & i).Value = "Approve with standard rates"
                ElseIf Range("B" & i).Value >= 24000 And Range("B" & i).Value < 40000 Then
                   Range("C" & i).Value = "Await manager's approval"
                Else
                   Range("C" & i).Value = "Reject application"
                End If
              Next i
            End With
        End Sub