更改另一个单元格的值

时间:2015-03-17 03:44:08

标签: excel vba excel-vba

我正在寻找Excel-Vba代码,根据另一个单元格的值直接在某个单元格中显示某个文本,让我详细说明。表格如下:

  1. 我将从单元格A3:A13
  2. 输入A栏中的成绩
  3. 代码必须单独打印相应的注释,如果a1 - 优秀,如果a2 - 良好的工作
  4. 此代码必须适用于40个等级条目范围内的条目
  5. Grades  ------          Comments
    a1      ------          excellent 
    b2      ------          work harder
    a1      ------          excellent
    b1      ------          satisfactory
    a2      ------          good work

1 个答案:

答案 0 :(得分:0)

以下代码会继续,直到找到A栏中尚未评分的学生:

Sub gradeComments()

    Dim grade As String, comment As String
    Dim i As Integer

    i = 3 ' your starting row

    Do While i > 0

      grade = Range("A" & i).Value

      If grade = "a1" Then
         comment = "Excellent"
      ElseIf grade = "a2" Then
         comment = "Good work"
      ElseIf grade = "b1" Then
         comment = "Satisfactory"
      ElseIf grade = "b2" Then
         comment = "Work harder"
      End If

      If grade = vbNullString Then
        Exit Do
      Else
        Range("B" & i) = comment
        i = i + 1
      End If
    Loop
End Sub