Macro to cut and paste multiple rows from one sheet to another, and deleting the now empty rows.

BAQI

New Member
Joined
Dec 2, 2022
Messages
40
Office Version
  1. 2016
Platform
  1. Windows
I have a spreadsheet where column "S" has a PO #, and column "V" has a received status. What I'm looking to do is the following:

If column "V" has an "R" (received), then it goes to column "S" to check the PO, and compares that to all of the PO #'s in column "S". If all of the rows with the matching PO have been received, it takes all of those rows, cuts them, and puts them to another sheet (so I can go back and reference down the line). It will also delete the now empty rows.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi and welcome to MrExcel

Your data on Sheet1, results on Sheet2.
Try this macro:

VBA Code:
Sub CutRows()
  Dim sh1 As Worksheet
  Dim dic As Object
  Dim i As Long, lr As Long, x As Long, y As Long
  Dim a As Variant, ky As Variant
  Dim rng As Range
  Dim po As String
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Sheet1")
  Set dic = CreateObject("Scripting.Dictionary")
  Set rng = sh1.Range("A1", sh1.Range("V" & Rows.Count).End(3))
  a = rng.Value
  
  For i = 1 To UBound(a, 1)
    po = a(i, 19)
    If UCase(a(i, 22)) = "R" Then y = 1 Else y = 0
    If Not dic.exists(po) Then
      x = 1
    Else
      x = Split(dic(po), "|")(0) + 1
      y = Split(dic(po), "|")(1) + y
    End If
    dic(po) = x & "|" & y
  Next
  
  For Each ky In dic.keys
    If Split(dic(ky), "|")(0) <> Split(dic(ky), "|")(1) Then
      dic.Remove ky
    End If
  Next
  
  rng.AutoFilter 19, dic.keys, xlFilterValues
  sh1.AutoFilter.Range.EntireRow.Copy Sheets("Sheet2").Range("A1")
  sh1.AutoFilter.Range.Offset(1).Delete
  sh1.ShowAllData
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi and welcome to MrExcel

Your data on Sheet1, results on Sheet2.
Try this macro:

VBA Code:
Sub CutRows()
  Dim sh1 As Worksheet
  Dim dic As Object
  Dim i As Long, lr As Long, x As Long, y As Long
  Dim a As Variant, ky As Variant
  Dim rng As Range
  Dim po As String
 
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Sheet1")
  Set dic = CreateObject("Scripting.Dictionary")
  Set rng = sh1.Range("A1", sh1.Range("V" & Rows.Count).End(3))
  a = rng.Value
 
  For i = 1 To UBound(a, 1)
    po = a(i, 19)
    If UCase(a(i, 22)) = "R" Then y = 1 Else y = 0
    If Not dic.exists(po) Then
      x = 1
    Else
      x = Split(dic(po), "|")(0) + 1
      y = Split(dic(po), "|")(1) + y
    End If
    dic(po) = x & "|" & y
  Next
 
  For Each ky In dic.keys
    If Split(dic(ky), "|")(0) <> Split(dic(ky), "|")(1) Then
      dic.Remove ky
    End If
  Next
 
  rng.AutoFilter 19, dic.keys, xlFilterValues
  sh1.AutoFilter.Range.EntireRow.Copy Sheets("Sheet2").Range("A1")
  sh1.AutoFilter.Range.Offset(1).Delete
  sh1.ShowAllData
  Application.ScreenUpdating = True
End Sub

Thank you for getting back to me!

1670243321243.png


So it appears there used to be another sheet, and I went ahead and renamed the ones that it was going to work on.

Also, I'm not sure if this matters, but the first row looks like the following, and has drop downs:

1670243389749.png


I tried to edit the code to fix the sheets, but it isn't working properly.
 
Upvote 0
You must adjust the names of your sheets in the macro.
If you have difficulty adapting the code, share your file on google drive, share the file to anyone who has the link, copy the link and paste it here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
You must adjust the names of your sheets in the macro.
If you have difficulty adapting the code, share your file on google drive, share the file to anyone who has the link, copy the link and paste it here. If the workbook contains confidential information, you could replace it with generic data.


Here's the link. I did try to adjust the macro with no success, so I believe it has just the original that you posted. Thank you for taking the time to look at it.
 
Upvote 0
Your file that you shared is empty, so I can't help you.
I made the adjustments to the sheet names in the macro.
Try the following code:


VBA Code:
Sub CutRows()
  Dim sh1 As Worksheet
  Dim dic As Object
  Dim i As Long, lr As Long, x As Long, y As Long
  Dim a As Variant, ky As Variant
  Dim rng As Range
  Dim po As String
  
  Application.ScreenUpdating = False
  Set sh1 = Sheets("Open")
  Set dic = CreateObject("Scripting.Dictionary")
  Set rng = sh1.Range("A1", sh1.Range("V" & Rows.Count).End(3))
  a = rng.Value
  
  For i = 1 To UBound(a, 1)
    po = a(i, 19)
    If UCase(a(i, 22)) = "R" Then y = 1 Else y = 0
    If Not dic.exists(po) Then
      x = 1
    Else
      x = Split(dic(po), "|")(0) + 1
      y = Split(dic(po), "|")(1) + y
    End If
    dic(po) = x & "|" & y
  Next
  
  For Each ky In dic.keys
    If Split(dic(ky), "|")(0) <> Split(dic(ky), "|")(1) Then
      dic.Remove ky
    End If
  Next
  
  rng.AutoFilter 19, dic.keys, xlFilterValues
  sh1.AutoFilter.Range.EntireRow.Copy Sheets("Received").Range("A1")
  sh1.AutoFilter.Range.Offset(1).Delete
  sh1.ShowAllData
  Application.ScreenUpdating = True
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (CutRows) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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