+ Reply to Thread
Results 1 to 17 of 17

need macro to compare column in one sheet to another sheet and if part # is not found copy

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2013
    Location
    Pittsburgh, Pa
    MS-Off Ver
    Excel 2003
    Posts
    7

    need macro to compare column in one sheet to another sheet and if part # is not found copy

    Need help writing a macro to simplify my work for my business. My excel sheets are part numbers in a column with descriptions, reference numbers and pricing info in its corresponding line.

    I need to be able to check column A in worksheet vendor part number against column A worksheet My part number and if vendor part number is not found in worksheet my part number then have it copy the new part number that wasnt found in worksheet my part number and all information in that line to worksheet new parts.

    I have been just simply copying the two columns and comparing and manually copying the information and then using find to find the part number in the vendors and manually copying each over. I deal with over 10,000 parts. Very time consuming. I have worked with formulas but have not as of yet written a macro. Need help with this please.

    Naunid

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Hi Naunid.

    Post a sample excel file.
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Hi,

    Haven't got time just at the moment to offer a macro, and as JRAJ has said we'd need a workbook.
    However as a stop gap measure check out the =MATCH() function. This allows you to find whether a value appears in another column. If not it will return a #VALUE result. So if you use an
    Formula: copy to clipboard
    =MATCH(A1,Sheet2!A$1:A$10000,False)
    type of formula and copy it down your 10000 rows, you can then filter the column for the #VALUE results and copy and paste the filtered values.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    01-19-2013
    Location
    Pittsburgh, Pa
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Please find attached a sample book of the worksheets.

    Thank you so much for your quick response, greatly appreciated.

    Naunid
    Attached Files Attached Files

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Hi,

    Try the following macro. The reason AB33 finds the looping macro slow when used with hundreds of rows is because each time through the loop VB code has to jump back to Excel and then jump back to the VB environment. There is a time overhead with each jump which is why loops should be avoided at all costs for this sort of stuff. The fastest way I know to achieve this is to use Excel's standard data filtering functionality. So:

    Sub CopyNewParts()
        Dim lLastrow As Long
        lLastrow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
        Sheet1.Range("AF1") = "NewPart"
        Sheet1.Range("AF2") = "=IFERROR(MATCH(A2,'My Part Number'!A:A,FALSE),""New"")"
        Sheet1.Range("AF2").Copy Destination:=Sheet1.Range("AF2:AF" & lLastrow)
        Sheet1.Range("AF1").AutoFilter , Field:=32, Criteria1:="New"
        If Sheet1.Range("A" & Rows.Count).End(xlUp).Row > 1 Then
            Sheet1.Range("a1").CurrentRegion.SpecialCells(xlCellTypeVisible).Offset(1, 0).Resize(, 31).Copy
            Sheet3.Range("A" & Rows.Count).End(xlUp).Cells(2, 1).PasteSpecial (xlPasteAll)
        End If
        Sheet1.Range("A1").AutoFilter
    End Sub

  6. #6
    Registered User
    Join Date
    01-19-2013
    Location
    Pittsburgh, Pa
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Hello Richard

    Thank you for responding. Ok may need a little more help here LOL. I created a new macro using what you had posted. I must of did something wrong. Please give me detailed instructions on how to put this in. I apparently did a boo boo LOL

    Thanks
    Naunid

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Hi,

    See attached.
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    I have used this code for a number of times, but almost came to conclusion that it is not suitable for a large data as it is very slow, but have a go.

    Sub find_copy()
    Dim c2 As Range, c As Range
    
    Application.ScreenUpdating = 0
    With Sheets("Vendor Part Number")
        For Each c2 In .Range("A2:A" & .Cells(Rows.Count, 3).End(xlUp).Row)
            Set c = Sheets("My Part Number").Columns(1).Find(c2, , xlValues, xlWhole)
            If c Is Nothing Then
            c2.EntireRow.Copy Sheets("New Parts").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End If
       Next c2
     End With
     
     Application.ScreenUpdating = 1
    End Sub

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Richard,
    You are right with the speed!

    The find function is looking for each cell. Looping through an array of dictionary would be fast, but dictionary could not cope with duplcation and not as flexible as the find function.
    I am aware filtering is much faster than looping, but I need to overcome my phobia of filtering

  10. #10
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Hi Nauhid.

    Just try this:

    parts.xlsm

    This is the code that does the task..
    Sub CopyNewParts()
        Dim rsht1 As Long, rsht2 As Long, routput As Long, cnt As Long
        
        rsht1 = Sheets("Vendor Part Number").Range("A" & Rows.Count).End(xlUp).Row
        rsht2 = Sheets("My Part Number").Range("A" & Rows.Count).End(xlUp).Row
        routput = Sheets("New Parts").Range("A" & Rows.Count).End(xlUp).Row
        cnt = 0
        
        For i = 2 To rsht1
            For j = 2 To rsht2
                If Sheets("Vendor Part Number").Range("A" & i) <> Sheets("My Part Number").Range("A" & j) Then
                    cnt = cnt + 1
                Else
                    cnt = 0
                    Exit For
                End If
            Next
            If cnt > 0 Then
                routput = routput + 1
                Sheets("Vendor Part Number").Rows(i).Copy (Sheets("New Parts").Range("A" & routput))
                cnt = 0
            End If
        Next
    End Sub

  11. #11
    Registered User
    Join Date
    01-19-2013
    Location
    Pittsburgh, Pa
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Thank you Thank You Thank You , What took me 14 hour days and Weeks just took 10 mins max.

    All the help is and always will be appreciated. I dropped 10,000 parts and descriptions into the vendors, put all my part numbers into the my parts and it gave me all the new parts that I do not have. As an owner of 2 online small businesses my time is very consumed and you just gave me time to breathe LOL

    thank you so much
    the best to all of you that helped
    Nauni

  12. #12
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: need macro to compare column in one sheet to another sheet and if part # is not found

    Happy to help Nauni..

    Please click the star below

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1