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.

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top