简体   繁体   中英

Change the value of another cell

I am looking for Excel-Vba code to directly show a certain text in a certain cell based on the value of another cell ,let me elaborate . The table will be as follows:

  1. I will input the grades in column A say from cells A3:A13
  2. The code must print the corresponding comment on its own ,like if a1 - excellent if a2 - good work
  3. This code must work for an entry of range of say 40 grade entries

Grades  ------          Comments
a1      ------          excellent 
b2      ------          work harder
a1      ------          excellent
b1      ------          satisfactory
a2      ------          good work

The following code continues until it finds a student in column A which has not been graded:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM