验证单元格并获得成绩

时间:2015-05-20 12:26:34

标签: excel excel-vba vba

这是我的要求。我有张B,其中B列有标记。根据标记,只需点击一下按钮,我就可以根据等级获得成绩并为单元格着色。以下是我写的代码,但这不成功。请帮助

Sub SetGrade()
    Dim r As Integer
    Dim Score As Double

    For r = 2 To 20
        Score = Cells(r, 2).Value

        If Score >= 90 And Score <= 100 Then
            ActiveCell(1, 2).Value = "A"
            ActiveCell(1, 2).Interior.ColorIndex = 4
            ActiveCell(1, 2).HorizontalAlignment = xlCenter
        ElseIf Score >= 75 And Score <= 90 Then
            ActiveCell(1, 2).Value = "B"
            ActiveCell(1, 2).Interior.ColorIndex = 46
            ActiveCell(1, 2).HorizontalAlignment = xlLeft
        End If
    Next r
End Sub

1 个答案:

答案 0 :(得分:3)

我认为你真正需要做的就是摆脱像这样的活跃单元:

Sub SetGrade()
Dim r As Integer
Dim Score As Double

For r = 2 To 20

Score = Cells(r, 2).Value

If Score >= 90 And Score <= 100 Then

    Cells(r, 3).Value = "A"
    Cells(r, 3).Interior.ColorIndex = 4
    Cells(r, 3).HorizontalAlignment = xlCenter

ElseIf Score >= 75 And Score <= 90 Then

    Cells(r, 3).Value = "B"
    Cells(r, 3).Interior.ColorIndex = 46
    Cells(r, 3).HorizontalAlignment = xlLeft

End If

Next r

End Sub