为什么在Excel VBA

时间:2017-01-12 12:01:04

标签: excel vba

我有一个Excel VBA功能,它根据A列中的标记给出B栏中学生的评分。我尝试使用FOR NEXT并且它工作正常,但是当我尝试使用DO时显示错误。这个问题在哪里?

Private Sub Button2_Click()
    Dim mark As Single
    Dim grade As String
    Dim counter As Integer
    counter = 1
    Do While counter < 10
        counter = counter + 1
        mark = Cells(counter, 1).Value
        'To set the alignment to center
        Range("A1:B10").Select
            With Selection
            .HorizontalAlignment = xlCenter
            End With
        Select Case mark
            Case 0 To 20
            grade = "F"
            Cells(counter, 2) = grade
            Case 20 To 29
            grade = "E"
            Cells(counter, 2) = grade
            Case 30 To 39
            grade = "D"
            Cells(counter, 2) = grade
            Case 40 To 59
            grade = "C"
            Cells(counter, 2) = grade
            Case 60 To 79
            grade = "B"
            Cells(counter, 2) = grade
            Case 80 To 100
            grade = "A"
            Cells(counter, 2) = grade
            Case Else
            grade = "Error!"
            Cells(counter, 2) = grade
        End Select
    Loop
End Sub

您可以在excel表A列的前10行中取0到100之间的任何值。当你运行宏时,B单元格会被评分填充。

2 个答案:

答案 0 :(得分:1)

请尝试以下操作。代码在下一行写入等级,因为在将等级写入行单元格之前计数器增加。我在select case之后移动了语句counter = counter + 1,现在所有的行都得到了相应的等级。

Private Sub Button2_Click()
    Dim mark As Single
    Dim grade As String
    Dim counter As Integer
    counter = 1
    Do While counter < 10            
        mark = Cells(counter, 1).Value
        'To set the alignment to center
        Range("A1:B10").Select
            With Selection
            .HorizontalAlignment = xlCenter
            End With
        Select Case mark
            Case 0 To 20
            grade = "F"
            Cells(counter, 2) = grade
            Case 20 To 29
            grade = "E"
            Cells(counter, 2) = grade
            Case 30 To 39
            grade = "D"
            Cells(counter, 2) = grade
            Case 40 To 59
            grade = "C"
            Cells(counter, 2) = grade
            Case 60 To 79
            grade = "B"
            Cells(counter, 2) = grade
            Case 80 To 100
            grade = "A"
            Cells(counter, 2) = grade
            Case Else
            grade = "Error!"
            Cells(counter, 2) = grade
        End Select
        counter = counter + 1
    Loop
End Sub

答案 1 :(得分:1)

您的代码适合我。 因为你想要在前10行中翻阅,你必须在开头设置counter = 0,或者根本不设置它 此外,你可以缩短它:

Private Sub Button2_Click()
    Dim mark As Single
    Dim grade As String
    Dim counter As Integer

    Range("A1:B10").HorizontalAlignment = xlCenter
    Do While counter < 10
        counter = counter + 1
        mark = Cells(counter, 1).Value
        'To set the alignment to center
        Select Case mark
            Case 0 To 19
                grade = "F"
            Case 20 To 29
                grade = "E"
            Case 30 To 39
                grade = "D"
            Case 40 To 59
                grade = "C"
            Case 60 To 79
                grade = "B"
            Case 80 To 100
                grade = "A"
            Case Else
                grade = "Error!"
        End Select
        Cells(counter, 2) = grade
    Loop
End Sub