简体   繁体   中英

Assign the values in a cell based on the input in another cell

Here is my code:

Private Sub Worksheet_Change(ByVal Target As Range)
  If Range("A1").Value = "ORACLE" Then
  Range("B1").Value = "DATABASE"
  ElseIf Range("A1").Value = "UNIX" Then
  Range("B1").Value = "SERVER"
  End If
End Sub

How do I do this for the entire column?

Try this:

Private Sub Worksheet_Change()
Dim rng As Range

Range("A1").Select
For Each rng In Range("A:A")
  If rng.Value = "ORACLE" Then
    ActiveCell.Offset(0, 1).Value = "DATABASE"
    ActiveCell.Offset(1, 0).Select
  ElseIf rng.Value = "UNIX" Then
    ActiveCell.Offset(0, 1).Value = "SERVER"
    ActiveCell.Offset(1, 0).Select
  End If
Next rng

End Sub

This does not rely on activecell and since it based on an event in column A this only changes the cell in column B if Column A changes.

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("A:A")) Then

    If Target.value = "ORACLE" Then
        Range("B" & Target.Row).value = "DATABASE"
    ElseIf Target.value = "UNIX" Then
        Range("B" & Target.Row).value = "SERVER"
    End If
End If
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