vba编码“do until or while”和“loop”

时间:2016-08-04 03:31:38

标签: vba loops

我的Excel文件中有3列:名称,百分比和成绩。

我希望自动化字母等级(例如95的百分比将生成“A”)以填充第三列直到最后一行。

我一直收到与如何循环此代码相关的错误。有什么见解吗?

Sub Grades()
    Dim score As Integer

    Dim x As Integer
    x = 1
    score = Sheets("sheet1").Cells(x, 2).Value

    Do While score <> ""

        If score >= 90 And score <= 100 Then
            Sheets("Sheet1").Cells(x, 3).Value = "A"

        ElseIf score > 79 And score < 90 Then
            Sheets("Sheet1").Cells(x, 3).Value = "B"

        ElseIf score > 69 And score < 80 Then
            Sheets("Sheet1").Cells(x, 3).Value = "C"

        ElseIf score > 59 And score < 70 Then
            Sheets("Sheet1").Cells(x, 3).Value = "D"

        ElseIf score < 60 Then
            Sheets("Sheet1").Cells(x, 3).Value = "F"

        Else
            Sheets("Sheet1").Cells(x, 3).Value = ""

  End If

 x = x + 1
 score = Sheets("sheet1").Cells(x, 2).Value

 Loop

3 个答案:

答案 0 :(得分:1)

问题是得分是一个数字,并且总是&lt;&gt; “”。

Sub Example2()
    Dim score As Integer
    Dim x As Integer
    x = 2

    With Sheets("sheet1")
        Do While .Cells(x, 2).Value <> ""

            score = .Cells(x, 2).Value

            If score >= 90 And score <= 100 Then
                .Cells(x, 3).Value = "A"

            ElseIf score > 79 And score < 90 Then
                .Cells(x, 3).Value = "B"

            ElseIf score > 69 And score < 80 Then
                .Cells(x, 3).Value = "C"

            ElseIf score > 59 And score < 70 Then
                .Cells(x, 3).Value = "D"

            ElseIf score < 60 Then
                .Cells(x, 3).Value = "F"

            Else
                .Cells(x, 3).Value = ""

            End If
            x = x + 1

        Loop

    End With
End Sub

答案 1 :(得分:1)

Petrie Manuel,我不确定您在什么情况下想要使用Else声明:

Sheets("Sheet1").Cells(x, 3).Value = "" 

因为如果得分低于60,你会把一个 F,不是吗?

无论如何,为了简化代码和逻辑语句,我使用选择案例

Sub Grades()

    Dim score       As Integer
    Dim x           As Integer
    Dim sht1        As Worksheet

    Set sht1 = ThisWorkbook.Worksheets("sheet1")

    ' if your first row is for table headers
    x = 2

    With sht1
        Do While .Cells(x, 2).Value <> ""
            score = .Cells(x, 2).Value

            Select Case score
                Case Is >= 90
                    .Cells(x, 3).Value = "A"

                Case Is >= 80
                    .Cells(x, 3).Value = "B"

                Case Is >= 70
                    .Cells(x, 3).Value = "C"

                Case Is >= 60
                    .Cells(x, 3).Value = "D"

                Case Is < 60
                     .Cells(x, 3).Value = "F"

                Case Else
                    .Cells(x, 3).Value = ""

            End Select

            x = x + 1
        Loop

    End With

End Sub

答案 2 :(得分:0)

自:

  • 列“B”中的数字是百分比 - &gt;总是在0到100之间

  • 您的需求是以10的倍数

  • 解析它们

然后你可以像下面那样:

Option Explicit

Sub Grades()
    Dim cell As range
    With Sheets("scores") '<--| change "scores" with your actual sheet name
        For Each cell In .range("B1", .Cells(.Rows.Count, "B").End(xlUp)).SpecialCells(xlCellTypeConstants, xlNumbers) '<--| loop through "numeric" column "B" cells down to last non empty one only
            cell.Offset(, 1) = Choose(Int(cell.value / 10) + 1, "F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A") 
        Next cell
    End With
End Sub

如果您的百分比来自公式,那么只需将xlCellTypeConstants替换为xlCellTypeFormulas