Consulting

Results 1 to 6 of 6

Thread: Delete Unique (non-duplicate) values by row

  1. #1
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    3
    Location

    (Solved) Delete Unique (non-duplicate) values by row

    Hi all,

    I have a fairly simple issue here, I think, but after some searching, haven't found exactly what I'm looking for. I should say that I do have some experience with VB .NET, but none at all with Excel macros.

    I have a .csv file from a vendor that contains product data for all of their products. I do not want all products imported into my database, and I have a list of product ids for the products that I do want imported. Therefore, what I want to do is compare the product_id column from each workbook and delete those rows (from the vendor's workbook) which do not have a matching product_id. Again, I'm sure this is quite simple, I just have no idea how to go about accomplishing it.

    Any help is greatly appreciated. Thanks!
    Last edited by Johnny B; 09-14-2008 at 05:48 PM.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Dim i As Long
    Dim LastRow As Long

    Application.ScreenUpdating = False

    With ActiveSheet

    iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = LastRow To 1 Step -1

    If IsError(Application.Match(.Cells(i, "A").Value, Range("ListOfProducts"), 0)) Then

    .Rows(i).Delete
    End If
    Next i

    End With

    Application.ScreenUpdating = True

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Look at Adavanced Filter.

    Put headers on the columns if they don't already have them.
    The whole range (many columns) of the vendor's workbook is the data range.
    Your one column of Product_ID is the Criteria range.
    That will show those product_ID's from the vendor's list that you are interested in.

    If you want to use Advanced Filter's "Copy to Another Location" feature, the sheet of the "other location" should be the active sheet when you press Advance Filter from the Ribbon.

  4. #4
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    3
    Location
    Thank you for the responses!

    Mikerickson - my first attempts at this aren't working, but I am looking into this right now.

    XLD - This is working!...except 3 to 4 products are deleted that should not be. I have checked the product ids of those that were wrongly deleted, and I'm fairly sure thats not the problem. I'm assuming iLastRow is a typo and should read LastRow. I have replaced "A" with the column containing the vendor product_ids. I have also replaced "ListOfProducts" with the product_ids to import...in this case reading "AJ2:AJ618". Is this correct?

    Again, thank you both...this really helps. Thanks.

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    AdvancedFilter requires no blank rows in either the data range or the Criteria range.

  6. #6
    VBAX Newbie
    Joined
    Sep 2008
    Posts
    3
    Location
    Mikerickson -- The Advanced Filtering is now working......you were correct, I had blanks in there, that was the problem I think. I like this method, it is quite convenient.

    I appreciate all the advice, this will save me a lot of time. Thank you Mikerickson, XLD, and everyone else who read this.

Posting Permissions

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