2017-08-26 14 views
0

次の2つのコードがあります。もう1つは、最初のコードを呼び出すために呼び出し関数を使用しようとしています。Excel VBA呼び出し関数とループ

私はRange( "e42:e48")のスコアがいくつかあります。

ただし、2番目のコードはスコアリストを通過せず、次のスコアがクリックされてコードが実行されるまで、各スコアを一度にチェックするだけです。

お手数をおかけしますようお願い申し上げます。ありがとう。新

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 
+1

正しく理解していれば、E42からE48までの各セルに対して 'IfElseIfTest_1'を実行します。その場合、いくつかのオプションがありますが、最も簡単なのは、おそらく2番目のSub – FernAndr

答えて

0

hereが述べたようにあなたがSELECTACTIVATEを使用しないようにしてください。以下のコードを試してください。

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 
+0

で 'Call IfElseIfTest_1'の前に' score.Select'を実行することです。これは完璧です。どうもありがとう。 –

+0

@xinpds - あなたはようこそ! – Mrig

関連する問題