1

我对 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

但是,当我尝试使用 call 功能时,

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 到底出了什么问题?

4

1 回答 1

0

进一步的评论。我为您准备了两个解决方案(还有更多,但这两个是我想到的第一个)。


  1. 用户定义函数
    一旦将函数粘贴到您的任何模块中,您可以通过单击单元格并写入 =LoanAppStatus(range),即 =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.重写你原来的子程序

'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
于 2013-03-14T08:49:19.510 回答