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. When I enter a date into a cell in column I, The row B:L on which I entered the date in column I will be copied and pasted into the the next available empty row of the range B:L. After the paste the contents of the original row will be cleared from column B to L, then all the rows below the recently cleared row will be moved up to fill in the space of the recently cleared row.
- Assistant
Here is a VBA code that will achieve your desired result:
Sub CopyAndClear() Dim ws As Worksheet Dim lastRow As Long Dim copyRange As Range Dim pasteRange As Range ' Set the worksheet Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with your sheet name ' Find the last used row in column I lastRow = ws.Cells(ws.Rows.Count, "I").End(xlUp).Row ' Check if a date is entered in column I If ws.Range("I" & lastRow) <> "" Then ' Set the ranges to copy and paste Set copyRange = ws.Range("B" & lastRow & ":L" & lastRow) Set pasteRange = ws.Range("B" & ws.Rows.Count, "L" & ws.Rows.Count).End(xlUp).Offset(1) ' Copy and paste the values copyRange.Copy pasteRange ' Clear the original row copyRange.ClearContents ' Move the rows below up ws.Range("B" & lastRow + 1 & ":L" & ws.Rows.Count).Copy ws.Range("B" & lastRow & ":L" & ws.Rows.Count).PasteSpecial xlPasteValues Application.CutCopyMode = False End If ' Clear the contents of column I ws.Range("I" & lastRow).ClearContents End Sub