My data range is B:L and starts at row 5. I want a Vba code that will do the following…
- User
My data range is B:L and starts at row 5. I want a Vba code that will do the following on Worksheet_SelectionChange. After I enter a date into a cell in column I identify the cell as my Target Cell, and the row from column B to column L as my Target Row. Find the last used row in column J using lastRow = ws.Cells(ws.Rows.Count, “J”).End(xlUp).Row. Copy and paste the Target Row into the the next available lastRow. After paste, clear the contents of the Target Row, then all the rows below the recently cleared Target should be moved up by 1 to fill in the space of the recently cleared Target Row.
- Assistant
You can use the following code to achieve the described functionality:
vbaPrivate Sub Worksheet_SelectionChange(ByVal Target As Range) Dim lastRow As Long Dim ws As Worksheet ' Check if the changed cell is in column I If Not Intersect(Target, Me.Range("I:I")) Is Nothing Then Set ws = ThisWorkbook.ActiveSheet ' Change this line to your sheet name or use specific sheet index ' Find the last used row in column J lastRow = ws.Cells(ws.Rows.Count, "J").End(xlUp).Row ' Copy and paste the Target Row into the next available lastRow ws.Range("B" & Target.Row & ":L" & Target.Row).Copy _ Destination:=ws.Range("B" & lastRow + 1 & ":L" & lastRow + 1) ' Clear the contents of the Target Row ws.Range("B" & Target.Row & ":L" & Target.Row).ClearContents ' Move up all the rows below the cleared Target Row by 1 ws.Range("B" & Target.Row + 1 & ":L" & lastRow).Cut _ Destination:=ws.Range("B" & Target.Row & ":L" & lastRow - 1) End If End Sub