How to Transfer a row to another sheet based on a value in a cell

Flavien

Board Regular
Joined
Jan 8, 2023
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Hello Experts!

How would you remove blank lines (cf. picture)? Please Help!

Public Sub CopyRows()
Sheets("EXTRACTION").Select
' Find the last row of data
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
' Loop through each row
For x = 2 To FinalRow
' Decide if to copy based on column G (A=1;E=5...)
ThisValue = Cells(x, 7).Value
If ThisValue = "Completed" Then
Cells(x, 1).Resize(1, 33).Cut
Sheets("Completed").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("EXTRACTION").Select
ElseIf ThisValue = "Rejected" Then
Cells(x, 1).Resize(1, 33).Cut
Sheets("Rejected").Select
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
Sheets("EXTRACTION").Select
End If
Next x
End Sub
 

Attachments

  • Capture.JPG
    Capture.JPG
    54.8 KB · Views: 16

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this:

VBA Code:
Sub CopyRows()
  Dim sh1 As Worksheet
  Dim arr As Variant, itm As Variant
  Dim lr As Long
  Application.ScreenUpdating = False
 
  Set sh1 = Sheets("EXTRACTION")
  arr = Array("Completed", "Rejected")
 
  On Error Resume Next: If sh1.FilterMode Then sh1.ShowAllData: On Error GoTo 0
  For Each itm In arr
    lr = sh1.Range("G" & Rows.Count).End(3).Row
    sh1.Range("A1:G" & lr).AutoFilter 7, itm
    With sh1.Range("A2").Resize(lr, 31)
      .Copy Sheets(itm).Range("A" & Rows.Count).End(3)(2)
      .EntireRow.Delete
    End With
    sh1.ShowAllData
  Next

  Application.ScreenUpdating = True
End Sub

Please consider the following.
Note Code Tag:
In future please use code tags when posting code.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
---------------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
--------------
Example:
Dante Amor
ABCDEFG
1No PlanIndiceDesignationClientCreateurDateProgress Status
2P01Abcl1fla08-enePending
3P02Bccl2fla09-eneCollected
4P03Cdcl3fla10-enePending
5P04Decl4fla11-eneCollected
6P05Efcl5fla12-eneCompleted
7P06Fgcl6fla13-eneRejected
8P07Ghcl7fla14-eneCompleted
9P08Hicl8fla15-eneRejected
10P09Ijcl9fla16-enePending
11P10Jkcl10fla17-eneCollected
12P11Klcl11fla18-enePending
13P12Lmcl12fla19-eneCollected
14P13Lmcl13fla20-eneCompleted
EXTRACTION
 
Upvote 0
Hello DanteAmor,

Thank you for your help!
I tried your macro but unfortunately either the rows are hidden or deleted even with "key words" in column G.

Finally this test allowed me to review my need. I would like to send only certain cells of the row in specific columns of my second table (Suivi) and then delete the copied rows.

Could you please help?

Please find enclosed an example. Sorry I couldn't use the macro “XL2BB”; I'll try again later! because I don't understand how it works.

Thanks a lot for your help!
 

Attachments

  • Extraction ERP.JPG
    Extraction ERP.JPG
    139.4 KB · Views: 27
  • Extraction Suivi.JPG
    Extraction Suivi.JPG
    126.6 KB · Views: 18
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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