简体   繁体   English

Excel VBA调用函数和循环

[英]Excel VBA Call Function and Loop

I have the following 2 codes, the second one is trying to use the call function to call the first code. 我有以下2个代码,第二个正在尝试使用call函数来调用第一个代码。

I have a few scores In Range("e42:e48"). 我在Range(“ e42:e48”)中有几分。

however, the second code does not go through the score list, it only check each score at a time until the next score is clicked and the code is run. 但是,第二个代码不会通过分数列表,它只会一次检查每个分数,直到单击下一个分数并运行该代码。

your help is greatly appreciated. 非常感谢您的帮助。 Thanks. 谢谢。 Xin Xin

Sub IfElseIfTest_1()
    Dim score As Integer
    score = activecell.Value

    If score >= 0 And score <= 35 Then
        activecell(1, 2).Value = "F"
        activecell(1, 2).HorizontalAlignment = xlCenter
        activecell(1, 3).Value = "Terrible - needs attention"

    ElseIf score >= 36 And score <= 50 Then
        activecell(1, 2).Value = "D"
        activecell(1, 2).HorizontalAlignment = xlCenter
        activecell(1, 3).Value = "Needs attention"

    ElseIf score >= 51 And score <= 65 Then
        activecell(1, 2).Value = "C"
        activecell(1, 2).HorizontalAlignment = xlCenter
        activecell(1, 3).Value = "Not bad, could do better"

    ElseIf score >= 66 And score <= 80 Then
        activecell(1, 2).Value = "B"
        activecell(1, 2).HorizontalAlignment = xlCenter
        activecell(1, 3).Value = "Good score"

    ElseIf score >= 81 And score <= 100 Then
        activecell(1, 2).Value = "A"
        activecell(1, 2).HorizontalAlignment = xlCenter
        activecell(1, 3).Value = "Excellent score, well done!"

    Else
        MsgBox "Score not valid"


    End If


End Sub






Sub IfElseIfTest_1_CallFunction()

    Dim score As Range

    ' need to Dim cell as RANGE,  IF AS STRING  WILL NOT WORK.


    For Each score In Range("e42:e48")

        Call IfElseIfTest_1

    Next score


End Sub

You should avoid using SELECT and ACTIVATE as mentioned here . 您应该避免使用此处提到的SELECTACTIVATE Try below code. 尝试下面的代码。

Sub IfElseIfTest_1(cel As Range)
    Dim score As Integer
    score = cel.Value
    If score >= 0 And score <= 35 Then
        cel.Offset(0, 1).Value = "F"
        cel.Offset(0, 2).Value = "Terrible - needs attention"
    ElseIf score >= 36 And score <= 50 Then
        cel.Offset(0, 1).Value = "D"
        cel.Offset(0, 2).Value = "Needs attention"
    ElseIf score >= 51 And score <= 65 Then
        cel.Offset(0, 1).Value = "C"
        cel.Offset(0, 2).Value = "Not bad, could do better"
    ElseIf score >= 66 And score <= 80 Then
        cel.Offset(0, 1).Value = "B"
        cel.Offset(0, 2).Value = "Good score"
    ElseIf score >= 81 And score <= 100 Then
        cel.Offset(0, 1).Value = "A"
        cel.Offset(0, 2).Value = "Excellent score, well done!"
    Else
        MsgBox "Score not valid"
    End If
    cel.Offset(0, 1).HorizontalAlignment = xlCenter
End Sub

Sub IfElseIfTest_1_CallFunction()
    Dim score As Range
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet4")  'change Sheet4 to your data sheet

    Application.ScreenUpdating = False        
    For Each score In ws.Range("e42:e48")
        Call IfElseIfTest_1(score)          'call IfElseIfTest_1 by passing range score
    Next score
    Application.ScreenUpdating = True
End Sub

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

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