VBA: Moving rows to a new sheet based on multiple criteria and delete from original sheet

simss6

New Member
Joined
Mar 29, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi there!

I have found similar threads on these forums but none that do exactly what I am trying to do.
I have a main sheet "Operators" that has multiple columns that I am try to pull data from into different sheets.

If column I="Recertify", then move entire row to "Recertify" sheet, pasting under the header/data that already is in the sheet and deleting row from the "Operator" sheet
If column G="-"or" "or a date before 1/1/22 and column H="completed" or "complete", then move entire row to "No QC" sheet, pasting under the header/data that already is in the sheet and deleting row from the "Operator" sheet
If column G= a date after 12/31/22 and Column H="Not enrolled" or "Not Started" or "In Progress" or "Incomplete, the move the entire row to "No Edu" pasting under the header/data that already is in the sheet and deleting row from the "Operator" sheet
If column G="-"or" "or a date before 1/1/22 and column H="Not enrolled" or "Not Started" or "In Progress" or "Incomplete, the move the entire row to "Neither" pasting under the header/data that already is in the sheet and deleting row from the "Operator" sheet

Hope that made sense. I really appreciate it!
 

Attachments

  • tempsnip.png
    tempsnip.png
    18.9 KB · Views: 71

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I think this will accomplish what you're looking for based off your description. Let me know if it needs adjustments:
VBA Code:
Option Compare Text

Sub RowMove()
Dim lrow1 As Long
Dim lrow2 As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long

Set ws1 = Sheets("Operators")
lrow1 = ws1.Cells(Rows.Count, 8).End(xlUp).Row

For i = lrow1 To 2 Step -1
    Set ws2 = Nothing

    If ws1.Cells(i, 9) = "Recertify" Then Set ws2 = Sheets("Recertify")
    If (ws1.Cells(i, 7) = "-" Or ws1.Cells(i, 7) < DateSerial(2022, 1, 1)) And (ws1.Cells(i, 8) = "completed" Or ws1.Cells(i, 8) = "complete") Then Set ws2 = Sheets("No QC")
    If (ws1.Cells(i, 7) = "-" Or ws1.Cells(i, 7) < DateSerial(2022, 1, 1)) And (ws1.Cells(i, 8) = "Not enrolled" Or ws1.Cells(i, 8) = "Not started" Or ws1.Cells(i, 8) = "In Progress" Or ws1.Cells(i, 8) = "Incomplete") Then Set ws2 = Sheets("Neither")
    If ws1.Cells(i, 7) >= DateSerial(2022, 1, 1) And (ws1.Cells(i, 8) = "Not enrolled" Or ws1.Cells(i, 8) = "Not started" Or ws1.Cells(i, 8) = "In Progress" Or ws1.Cells(i, 8) = "Incomplete") Then Set ws2 = Sheets("No Edu")
    
    If ws2 Is Nothing Then GoTo Nexti

    lrow2 = ws2.Cells(Rows.Count, 8).End(xlUp).Row + 1

ws1.Cells(i, 1).EntireRow.Copy ws2.Cells(lrow2, 1).EntireRow
ws1.Cells(i, 1).EntireRow.Delete

Nexti:
Next i

End Sub
 
Upvote 0
The recertify and the No QC work perfectly, the "Neither and the "No edu" all went into the "No edu" sheet.

This is awesome though, thank you, thank you!
 
Upvote 0
Interesting, there must be an issue with the dates all reading after 1/1/2022. Could you attach a screenshot of a row that should be going to the "Neither" sheet?
 
Upvote 0
Those last four that have the "-" listed as the last QC test, column "G"
 

Attachments

  • tempsnip1.jpg
    tempsnip1.jpg
    53.5 KB · Views: 18
Upvote 0
I'm unsure why, but for some reason this is firing as true when the date = "-"
VBA Code:
ws1.Cells(i, 7) >= DateSerial(2022, 1, 1)


I slightly edited the code to account for this:
VBA Code:
Option Compare Text

Sub RowMove()
Dim lrow1 As Long
Dim lrow2 As Long
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long

Set ws1 = Sheets("Operators")
lrow1 = ws1.Cells(Rows.Count, 8).End(xlUp).Row

For i = lrow1 To 2 Step -1
    Set ws2 = Nothing

    If ws1.Cells(i, 9) = "Recertify" Then Set ws2 = Sheets("Recertify")
    If (ws1.Cells(i, 7) = "-" Or ws1.Cells(i, 7) < DateSerial(2022, 1, 1)) And (ws1.Cells(i, 8) = "completed" Or ws1.Cells(i, 8) = "complete") Then Set ws2 = Sheets("No QC")
    If (ws1.Cells(i, 7) = "-" Or ws1.Cells(i, 7) < DateSerial(2022, 1, 1)) And (ws1.Cells(i, 8) = "Not enrolled" Or ws1.Cells(i, 8) = "Not started" Or ws1.Cells(i, 8) = "In Progress" Or ws1.Cells(i, 8) = "Incomplete") Then Set ws2 = Sheets("Neither")
    If ws1.Cells(i, 7) <> "-" And ws1.Cells(i, 7) >= DateSerial(2022, 1, 1) And (ws1.Cells(i, 8) = "Not enrolled" Or ws1.Cells(i, 8) = "Not started" Or ws1.Cells(i, 8) = "In Progress" Or ws1.Cells(i, 8) = "Incomplete") Then Set ws2 = Sheets("No Edu")
    
    If ws2 Is Nothing Then GoTo Nexti

    lrow2 = ws2.Cells(Rows.Count, 8).End(xlUp).Row + 1

ws1.Cells(i, 1).EntireRow.Copy ws2.Cells(lrow2, 1).EntireRow
ws1.Cells(i, 1).EntireRow.Delete

Nexti:
Next i

End Sub
 
Upvote 0
Solution
YOU ARE AMAZING! Thank you so much for all of your help. Everything is working properly.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top