2016-08-04 58 views
0

我在我的Excel文件中有3列:名稱,百分比和等級。vba編碼「做直到或同時」和「循環」

我想自動化字母等級(例如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 
+1

你得到什麼錯誤替代xlCellTypeConstants? –

+0

'Select Case'會更好的恕我直言。 –

回答

1

問題是,得分是一個數字,並將始終是<>「」。

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

皮特里曼努埃爾,我不知道你想用你的Else聲明什麼情況:

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

因爲如果分數低於60,你會把女,不是嗎?

無論如何,爲了簡化您的代碼和邏輯陳述我使用了選擇案例

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 
0

因爲:

  • 數列 「B」 是百分比 - >始終介於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 

應您的百分比來自於公式然後只需用xlCellTypeFormulas

+0

@PetrieManuel:你通過了嗎? – user3598756

+0

@PetrielManuel:給你嘗試和幫助你的人提供feedbakcs會很好 – user3598756