I have a dataset that contains a bunch of information on customers. I want this information moved into another worksheet when Yes
is entered into the Cell in row K
, and the original row containing the information to be deleted from the first worksheet
I'm doing this with a button press. Works fine, and rows were being deleted, but of course when it deletes the row then the row numbers shift up by one and so the next row is skipped over (row 15 is deleted and then row 16 becomes row 15 etc.) so I figured a reverse loop is the way to go, but I can't for the life of me figure this out - I thought it would be simple! Here's the code I'm using:
Private Sub UpdateSheet2_Click()
Dim c As Range
Dim Source As Worksheet
Dim Target As Worksheet
'Change worksheet designation as needed
Set Source = ActiveWorkbook.Worksheets("Sheet1")
Set Target = ActiveWorkbook.Worksheets("Sheet2")
For Each c In Source.Range("K:K") 'Covers all rows
If c = "yes" Then
Source.Range("A" & c.Row & ":B" & c.Row).Copy Target.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
Source.Range("D" & c.Row & ":F" & c.Row).Copy Target.Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
Source.Range("H" & c.Row & ":J" & c.Row).Copy Target.Range("F" & Rows.Count).End(xlUp).Offset(1, 0)
End If
' If c = "yes" Then
' Source.Rows(c.Row).EntireRow.Delete
' End If
Next c
For i = 500 To 1 Step -1
If Source.Range("K" & i) = "yes" Then
Source.Rows(i.Row).EntireRow.Delete
End If
Next i
End Sub
I'm just setting the row numbers between 1 and 500 for now, I will alter this when I have the basic functionality working. You can see where I commented out the original deletion method in the For Each loop. I'm currently getting the error "Object required" on the line Source.Rows(i.Row).EntireRow.Delete