Question

In an excel file when user selects a value in column 2, I am setting a value in column one based on the selection. I have written that code in the Private Sub Worksheet_Change(ByVal Target As Range) method.

But when user copy and paste multiple lines the value is set only for the first line in the copy range. Anybody have an idea to set the all the values.

Was it helpful?

Solution

You can use a "for" loop to go through all cells in the target.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cel As Range

If Not Intersect(Target, Range("B:B")) Is Nothing Then
    For Each cel In Target
      cel.Offset(0, -1) = "Some Value"
    Next cel
End If
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top