score:2
Accepted answer
Always use
Option Explicit
There is nothing called xlToUp
correct enum value is xlUp
This is wrong
Sub DeleteRow(Row As Long)
If RowsToDelete > 0 Then
Rows(Row).EntireRow.Delete Shift:=xlToUp
End If
End Sub
There is no RowsToDelete
variable so your condition always evaluates to false.
Correct code will be
Sub DeleteRow(RowsToDelete As Long)
If RowsToDelete > 0 Then
Rows(RowsToDelete).EntireRow.Delete Shift:=xlUp
End If
End Sub
Enable events after deleting the Row else you will get stuck in infinite loop.
Call DeleteRow(RowToDelete)
Application.EnableEvents = True
Always set CutCopyMode=False
after cut or copy
This will work.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim LastRowCompleted As Long
Dim RowToDelete As Long
RowToDelete = 0
LastRowCompleted = Sheets("completed").Cells(Sheets("completed").Rows.Count, "A").End(xlUp).Row
LastRowCompleted = LastRowCompleted + 1 'Next row after last row
Set KeyCells = Range("I:I")
Application.EnableEvents = False
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
'Cut and Paste Row
Target.EntireRow.Cut Sheets("completed").Range(LastRowCompleted & ":" & LastRowCompleted)
Application.CutCopyMode = False
'Mark to delete row
RowToDelete = Target.EntireRow.Row
End If
Call DeleteRow(RowToDelete)
Application.EnableEvents = True
End Sub
Sub DeleteRow(RowsToDelete As Long)
If RowsToDelete > 0 Then
Rows(RowsToDelete).EntireRow.Delete Shift:=xlUp
End If
End Sub
Source: stackoverflow.com
Related Query
- Delete and shift cells up on a worksheet change
- Change multiple named cells and ranges in a worksheet at once
- How to shift an excel UsedRange to begin at cell A1 its worksheet (i.e. delete all blank rows and columns outside UsedRange)?
- If there is a 0 in column K, delete rows C to K and shift cells up
- If any cells in column B contains string X, delete row and shift cells up
- Excel VBA Delete and Shift Up cells but only in a certain range
- excel macro: Delete row with 1 or more blank cells and shift data up
- VBA Excel How to delete filter blank rows (but not entire row) and shift cells up
- Delete worksheet if it exists and create a new one
- Word VBA: Macro to change cells in selection, and create a summary table of the tables?
- For each row, copy specific cells by predefined columns and paste to values into alternate predefined columns in a seperate worksheet
- Delete a worksheet and recreate with the same sheet name
- Loop through cells and change font
- Data Validation and Worksheet Change Event
- Loop trought my rows and delete row if 2 cells contains any value
- Why delete and recreate a querydef object when you can just change the .SQL property?
- Check if worksheet in workbook has specific name, and change it if it does
- Find cells of certain length and copy to current worksheet
- VBA worksheet change with multiple cells
- Find and delete duplicate cells in rows, not columns
- Record copy paste and delete for multiple cells in Audit Trail
- Looking for specific contents in each of the cells in the column and delete the row in some cases
- looping through worksheet command buttons and change visibility
- How to delete added value in cells in Excel Worksheet
- Deleting row elements except column1 and then shift cells up for the rest
- Cut, paste and shift a range one row up when delete button "btnS2" is clicked
- How to delete a column of a table and shift the rest to the left without affecting the previous tables in excel vba
- Excel VBA - Copy certain cells and paste next to other cells in same worksheet
- Copy value-only cells in multiple worksheet columns and paste into one column
- Loop through range of cells and change color if a value is in that range more then 3 times
More Query from same tag
- How to clear cells in range that are of currency format without looping
- Unable to read multiple web pages
- How to load embedded object in webbrowser control of a userform?
- Save sheet to TXT with UTF-8 encoding
- Import Unicode characters from txt file to MS-Word with VBA
- Public Static variable in excel vba
- VBA Macros: Unable to reset recordset in a loop of sql insert query statements (Error: Runtime Error - Automation error and unspecified error)
- Splitting a column by consecutive zeros
- Python pandas merge and save with existed sheets
- Delete entire row if cell contains the string X
- Understanding the PDF DOM
- Resize column width of a particular column
- Class for Custom Button Event Handling
- Excel Activex Listbox to open and close on selection of same cell without needing to click out onto another cell first
- Copying a value down into inserted blank rows over multiple columns
- How to interpret the VBA "with" function structure into R code via RDCOMClient?
- Run a python code (Anaconda3) using a VBA button in the Excel input workbook
- No macro for Power Query in Excel 2013
- Reversed equal sign
- Error - Invalid request. To select a shape its view must be active
- How to parse XML using vba
- Worksheet_Change & Range User Cell Protection
- Print all worksheet data into userform textbox
- Loop to unfilter multiple excel workbooks VBA
- SQL statement in VBA Excel
- Dynamic range with hidden worksheet name
- Import multiple lines of multiple HTML files, into cells (below each other) in one Excel file
- Validate userform inputs where controls are dynamic using MVP approach
- VBA For Each Loop only excutes once and then stops
- Setting the Recordset property of an Access form