VBA code needed for Copy/Paste SheetA to SheetB upon search input

Latteeeee

New Member
Joined
Jan 9, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
very new to VBA but I need a macro that 1)searches for a specific input 2)goes through SheetA(raw data) through the same column to search for input 3) recognize the input search and 4) copies the entire row into SheetB
Conditions:
1) Raw data file will always be different, but the column that input is searched upon will always be the same
2) There might be multiple results for the same search, meaning multiple rows, but each rows will contain varying data
3) Able to not repeat the copy/paste from SheetA to SheetB and not miss out on any rows/searches

*Unsure if xlookup is the way to go in the VBA code
Will really appreciate any help I can get!!

DelMacro2.xlsm
ABCD
1NameItemDate in Date out
2Abc1391003-07-2304-01-23
3Asd1928304-07-2305-01-23
4Aft1421305-07-2306-01-23
5Ayz1391006-07-2307-01-23
6AhE1433107-07-2308-01-23
7
8RawData
RawData


DelMacro2.xlsm
ABCDE
1Search13910<-- Item input cell
2
3NameItemDate outDate in
4
5
6
7^Data from RawData expected to be populated
Form
 
That's quite a jumble of the order ;)
Personally, I would use a custom sort, left to right, to get all the columns in the right order before the copy. I think I've got the order right - try (again) on a copy of your actual data. Copy all of the code below to a standard mode (uncomment the delete line if you want to delete chosen data after the copy).

VBA Code:
Option Explicit
Dim ws1 As Worksheet, ws2 As Worksheet
Sub DelMacro3()
    Application.ScreenUpdating = False
    Set ws1 = Worksheets("Delivery")
    Set ws2 = Worksheets("Del")
    Dim myInput As String, x As String
    myInput = ws2.Range("E1").Value2
    x = "X"
  
    'Clear existing data on Form sheet
    ws2.Range("A2:J" & Cells(Rows.Count, "A").End(xlUp).Row).Offset(1).ClearContents
 
    'Copy (then delete?) any data that matches
    With ws1.Cells(1, 1).CurrentRegion
        .AutoFilter 6, myInput
        If ws1.Cells(Rows.Count, 1).End(xlUp).Row > 1 Then
            .Range("A1:A2").EntireRow.Insert
            .Range("A1:V1").Offset(-2).Value2 = Evaluate("Column(" & .Address & ")")
            .Range("A1:V1").Offset(-1).Resize(1, 22).Value2 = Array(x, x, x, 7, x, _
            5, x, x, x, x, 1, 6, 2, 3, 4, 8, x, 9, 10, x, x, x)
            Sort_New_Order
            .Offset(1).Resize(.Rows.Count - 1, 10).Copy ws2.Range("A3")
          
            '.Offset(1).EntireRow.Delete   '<<*** uncomment this if you want to delete the copied data
          
            Sort_Old_Order
        Else
            MsgBox "No Items match " & myInput & " - exiting sub"
            .AutoFilter
            Exit Sub
        End If
        .AutoFilter
    End With
    ws2.Activate
    Application.ScreenUpdating = True
End Sub
Sub Sort_New_Order()
    ws1.Activate
    With ws1
        .Sort.SortFields.Clear
        .Cells(1, 1).CurrentRegion.Sort Key1:=Rows(2), Order1:=xlAscending, Orientation:=xlLeftToRight
    End With
End Sub
Sub Sort_Old_Order()
    With ws1
        .Sort.SortFields.Clear
        .Cells(1, 1).CurrentRegion.Sort Key1:=Rows(1), Order1:=xlAscending, Orientation:=xlLeftToRight
        .Rows("1:2").Delete
    End With
End Sub
The code works the first time when I run through the process. As I have another macro ("Import Button") that will allow me to import the raw data file first into Delivery sheet.
Upon the first search, it works
But as I copy/paste another search in, ran into a bug.
It seems the filtering is...making it harder? Do you know how to make the code reset Delivery sheet to its original state? perhaps that might resolve the issue. I'm not very sure how I can undo the filtering/reset the filter properly so delivery sheet returns to the original state
If I choose to not do the filtering/sorting, will it be simpler? cuz I can always add another button to sort afterwards.
Below is the mini sheet for what happens after the first search is completed.

DelMacro.xlsm
ABCDEFGHIJKLMNOPQRSTUV
1
2
3Overall credit statusSold-toShip-to name1Ship-toShip-to landCPNSales ProductFinished ProductAllocation PolicySales OrderDeliveryQty.Delivery Creation DatePlanned GI DateActual GI DateIncotermLast Leg Forwarder No.Last Leg Forwarder NameLast Leg AWB No.Sp DescriptionPlantPOD Date/Time(Last Leg)
Delivery
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I really can't do much more without getting access to your actual file, including this other macro you're now mentioning. Could you share via Dropbox, Google Drive or similar, and I'll take another look at it tomorrow.
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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