VBA to VLookup order# in master data, copy and paste rows in another sheet, then delete rows from master

TheGrandPooba

New Member
Joined
Jul 1, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello all, this is my first post, and feedback would be appreciated.

I would like to create a button below a standalone "Completed Order" cell to vlookup the order# in the master data sheet. The master table is named 'OImport,' and the order# header is [Order'#]. For any table rows that have the Completed Order#, I would like to copy and paste them in an 'OrderHistory' table on another sheet, then delete those rows from 'OImport.'

A couple points to note: The "Completed Order" cell would be manually typed in 'Summary' sheet G3, then the button would be clicked. Also, the order# could be in 1-5 rows in the master data sheet depending on the number of item's ordered. Lastly, after the rows are copy/pasted and deleted, could the "Completed Order" cell be cleared? ('Summary' G3)

Appreciate any help, I'm fairly new to the VBA language and couldn't find this particular request online.
Thanks!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
As you can see, it left only the orders with 266835, but didn't delete them. The OrderImport can be up to 500 rows long, so I couldn't copy the table using XL2BB. As you can see in the second photo, there is other data to the right of the table that I'm guessing is preventing the OImport rows from deleting.

Screenshot (4).png


Screenshot (5).png

I can just unhide the rows in OrderImport to undo the break in the macro, but if there was a way for only the table rows to be deleted, I believe your macro would execute properly (on my end😅.)
Thanks for troubleshooting with a total beginner, you are a life saver
 
Upvote 0
This appears to work for me:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "G3" Then Exit Sub
    Dim srcWS As Worksheet, desWS As Worksheet, rng As Range
    Set srcWS = Sheets("OrderImport")
    Set desWS = Sheets("OrderHistory")
    Application.ScreenUpdating = False
    With srcWS.ListObjects("OImport")
        .Range.AutoFilter 1, Target.Value
        .DataBodyRange.SpecialCells(xlCellTypeVisible).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1)
        Set rng = .DataBodyRange.SpecialCells(xlCellTypeVisible).Cells
        .DataBodyRange.AutoFilter Field:=1
        rng.Delete
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
This appears to work for me:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "G3" Then Exit Sub
    Dim srcWS As Worksheet, desWS As Worksheet, rng As Range
    Set srcWS = Sheets("OrderImport")
    Set desWS = Sheets("OrderHistory")
    Application.ScreenUpdating = False
    With srcWS.ListObjects("OImport")
        .Range.AutoFilter 1, Target.Value
        .DataBodyRange.SpecialCells(xlCellTypeVisible).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1)
        Set rng = .DataBodyRange.SpecialCells(xlCellTypeVisible).Cells
        .DataBodyRange.AutoFilter Field:=1
        rng.Delete
    End With
    Application.ScreenUpdating = True
End Sub
Thank you so much mumps!!! It fully cleared off of OrderImport and into OrderHistory :D
For the last time, thanks again for the persistence and being willing to accomodate my large request.
Thank you!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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