Re: Banging my head against a VBA Brick wall. Copy value to different sheet on cell change.
Well, I count 7 fields in your example from post
#5 above, but you also say you have data up to column K which is at least 11 columns because you wrote:
"The value from Sheet 1 A:A should go to Sheet2 A:A, value from Sheet 1 J:J to Sheet2 B:B and Sheet 1 K:K to Sheet2 C:C"
Assuming that what you wrote as quoted here is what you really have, then see the code I added to your existing procedure that theoretically should do what you seem to say you want.
Private Sub Worksheet_Change(ByVal Target As Range)
'Update 20140722
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("J:J"), Target)
xOffsetColumn = 1
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Code:
Dim lngTargetRow as Long, lngNextRow as Long
lngTargetRow = Target.Row
With Sheets("Sheet2")
lngNextRow = .Cells.Find(What:="*", After:=.Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
.cells(lngNextRow, 1).Value = cells(lngTargetRow, 1).Value
.cells(lngNextRow, 2).Value = cells(lngTargetRow, 10).Value
.cells(lngNextRow, 3).Value = cells(lngTargetRow, 11).Value
End With
Application.EnableEvents = True
End If
End Sub