+ Reply to Thread
Results 1 to 26 of 26

Detecting duplicates in whole column by VBA

  1. #1
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Detecting duplicates in whole column by VBA

    Greetings from Latvia, and i hope, ill find a good solution or any useful help in this forum.


    At the moment im making a small project in VBA Excel for creating bills and invoices, so this is where my storry begins.
    Thefore, Sheet1 in my excel workbook is a sheet, where the serial numbers and names of invoices and bills are stored.
    Numbers are taken from Sheet2 from a specific cell, where a user must enter a name for invoice or bill. There are no specific terms by entering name (no criterias), it can be as simple as INV-01 or BILL- 22 or something like that

    So i created a macro, that works like this-

    when the button is pressed, macro takes 3 or more values from Sheet2 cells and stores value of these cells in Sheet1 in specified cells
    No problems so far.

    What i want to do -

    I want to check entire Sheet1 column A for duplicates so the user cannot enter the same number of invoice twice in Sheet2 (actually it will be possible but it wont go further to store these values in Sheet1)

    could probably look like:

    Sub Duplicate check ()

    .... ..... ....


    If ..... then Duplicates found Else Continue

    End Sub



    'i will create a modules named "duplicates found" wich will pop out a Msgbox "Duplicates found" and reset a cell value of invoice number in Sheet2 and no values will be stored in Sheet1 and a second module Continue (to proceed if there are no duplicates) (no problems to make this)

    The problem is, i have no idea, what is the code for checking duplicate values (it may sound dumb, cause i've done much more complicated things in this project already)

    Any ideas?

    P.s. sorry for my English
    Last edited by Soilwork; 07-26-2014 at 03:40 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    Try
    Please Login or Register  to view this content.
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Looks good, will try and report later.

  4. #4
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Nope, there's a bug in it (or a bug in my brains )


    At first - shouldn't the beginning be like by the proper code rules:

    Dim CheckVal As Integer
    Dim MyValue As Integer



    Oh, if i get it right, the Option Explicit already does it?
    ____________________________

    If i try to run code, it says - Compile error End If without block If
    If i remove line End if then code always does the Else (Continue) macro no matter what values are in Sheet1 column A
    Last edited by Soilwork; 07-26-2014 at 06:03 PM.

  5. #5
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: Detecting duplicates in whole column by VBA

    Why not to place a code for worksheet change event on sheet2 where user enters invoice numbers and let all other codes remain same, so that if user re-enters an invoice number, user will get a message that you have entered a duplicate value and will be forced to change it.
    You may place this code on Sheet2 (not on a module) by clicking on Sheet2 Tab --> View Code --> and paste the code given below in the code window.
    Please Login or Register  to view this content.
    Does this help?
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  6. #6
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    sktneer,

    whoa, your code looks much more complicated but i have no doubt it works as fine it should (hasnt tested yet but i will)
    Idea bout user getting a message at the moment value is entered is great, thanks a lot.

    Im not actually a programmer, so i bet, i dont have what you guys call "a programmers way of thinking" - just some basic skills in VBA and VB

    Anyway, works this code for me or not, its 2AM in here, so ill get back to this tomorrow (yawn)
    Will report in any case.

  7. #7
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    This works like charm, also i forgot to mention one thing -

    as i wrote, when the button is pressed in Sheet2, macro takes 3 or more values from Sheet2 cells and stores value of these cells in Sheet1 in specified cells. One of these values is an invoice number to be checked for duplicates. Also under this button ) there is a code for generating and storing Sheet2. in PDF format. No problems with this, everything works.

    So i need to make a specific rule to add to sktneer's code - if the duplicates are detected, macro doesnt go any further and making PDF part is being canceled. So i guess it should look like this:

    So the sequence - Button in Sheet2 is pressed and first line of code is :

    Sub StoreNumber()
    With Sheets

    Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1).Value = Sheet2.Range("J8").Value
    End With

    Sheet2.Activate
    End Sub


    So now comes the sktneer's code and checks for duplicates in column A

    .... ...........
    ....... .......
    ........
    If n > 1 Then
    MsgBox Target & " is Duplicate Value. Please Try Again."
    Target = ""
    Target.Select
    End If
    End If
    End Sub


    I want to assign MsgBox "Duplicate Value" as new module Duplicate and the second module MakePDF

    so it should look like -
    ....... ....
    ......
    If n > 1 Then
    Duplicate
    Else
    MakePDF
    Target = ""
    Target.Select
    End If
    End If
    End Sub


    But this doesnt work - PDF is being created even the Duplicate runs

    I guess im wrong somewhere in the sequence

  8. #8
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Bump

    also, looks like im getting back to idea to put this code on a specific module, not in Sheet1 as sktneer suggested, so i could launch it by pressing a button

  9. #9
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    First, I do see why the previous code sent got an error ...
    Now, try next one and see in the file attached how it's running.
    For more information send a short sample of your data
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by PCI; 07-27-2014 at 06:18 PM.

  10. #10
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Phew, looks like its going to reach final destination, but there is a one last problem left

    Please Login or Register  to view this content.
    I need to delete row values in case if CheckVal >= 2
    Please Login or Register  to view this content.
    this doesnt work

    the root of evil is, that invoice numbers couldnt be entered manually in Sheet1 (its the order of customer), for myself, i use this project for making bills for my own as it was in beginning when i just started to make it, no automatically saved numbers, no warnings and so on. Then once i showed this to a friend of mine, and he said - wow, i need this, lets make a good deal, but i need just few more things like .... ...
    Last edited by Soilwork; 07-28-2014 at 04:03 PM. Reason: update

  11. #11
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    Is it what you want ??
    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Yes, except one thing


    Macro puts Range value in Sheet1 cells B1 (+1 cell down each next time button is pressed)
    But then the duplicate check code kicks in and checks compares the range and Sheet1 B cells, of corse values are equal at this moment so "Duplicate value" msgbox runs, yada yada yada

    Please Login or Register  to view this content.
    it worked fine for me but i just needed to add delete row function in case the duplicate is detected. Nothing else

  13. #13
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    I'm not sure to get it right.
    In the code I sent there is a Select statement and
    when CheckVal is 0 then there is no duplicate
    else if equal 1 there is a duplicate to come with the new value
    and else (= 2, 3 ..) it search for the first value and delete row.
    Here again send a sample of your file
    You have to use the FIND statement to know where is the first duplicate

  14. #14
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Problem solved - figured it out my way, as i said, im not a proggrammer and its hard to explain my needs in foreign language and by just describing them, phew.

    So -

    Please Login or Register  to view this content.

    and here is

    Please Login or Register  to view this content.
    And thats all i wanted to create

    Short explanation -

    A radio button transfers 3 values of Sheet3 (Invoice) cells to sheet1 (Number info storage) cells (inv. number, date, customer) and creates a pdf file in a specific directory on hard drive.

    When the values are transfered, sub cheks, if there any duplicates when you hit a radio button in Sheet3 again - so the procedure runs correctly at next data transfer, (when you hit a radio button to copy new data from sheet3 to Sheet1)

    If there is no duplicates then MsgBox "Invoice saved" and save as pdf code in another module runs
    If there any, they're beeing deleted on fly and user gets a warning and is forced to change Invoice number to much criteria (no duplicates)


    Thanks a lot guys, without your help i would never dig throught this

    Last edited by Soilwork; 07-29-2014 at 03:26 PM.

  15. #15
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    I'm please it's working
    Just to close the loop try
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    P.s.

    If the invoice number wouldnt be just a number, for example "5" but "INV 5"
    Should i define MyValue as String and will the Countif in code work properly? (if i remember right, countif counts only numbers)

  17. #17
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    P.s.

    If the invoice number wouldnt be just a number as for example "5" but like this "INV 5"
    should i dim MyValue as String and will the countif work properly? (i guess countif counts only numeric values)

  18. #18
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    Just use
    Please Login or Register  to view this content.
    and MyValue is Variant and can be : Number, text

  19. #19
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Ok, will try
    Sorry bout doublepost - didnt see the page 2#

  20. #20
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Nope, if i use Dim MyValue
    it allows to invoice number look like INV1 but the criteria doesnt work - it doesnt count duplicate values (no errors) and the sub deleterows doesnt work
    Last edited by Soilwork; 07-29-2014 at 06:07 PM.

  21. #21
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    Because strings are used there is an adjustment to do when counting cells
    Use next code, pay attention to the number of "
    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Yes!!

    Finally im ok with what i get.

    10x10010 thank you guys, and i hope, someone will find this very useful some other time

  23. #23
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    Good news.
    You're welcome

  24. #24
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    https://www.youtube.com/watch?v=evJSsJ0N-V8

    quality is *** and sorry for annoying background music but this is, how my entire project looks like

    (Language is Latvian) so it looks weird and misunderstandable for English or some other language speaking users

  25. #25
    Registered User
    Join Date
    07-26-2014
    Location
    LATVIA
    MS-Off Ver
    Office 2007
    Posts
    19

    Re: Detecting duplicates in whole column by VBA

    Hello again,

    time goes by and problems start to pop out -

    my customer changed OS from Windows 7 to 8.1 and Office 2007 to Office 365

    Everythin is fine with macros and stuff, but the problem is -

    in a cell, where a value is displayed in text, for example 66, - Sixty six Euros 00 cents, as should be, now it becomes Si%ty Si% Euros 00 cents

    Something to deal with Unicode?

    actuall its should be -
    Viens tūkstotis seši simti septiņdesmit astoņi eiro, 98 centi
    but it is
    Viens tûkstotis seði simti septiòdesmit astoòi eiro, 98 centi

  26. #26
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Detecting duplicates in whole column by VBA

    You have better to open a new thread to restart with a new topic, else people will miss you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Formula for detecting inexact duplicates?
    By Chucky2222 in forum Excel General
    Replies: 10
    Last Post: 06-21-2012, 08:53 PM
  2. Detecting / Avoiding Duplicates in a VBA userform.
    By noxios in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 11-18-2011, 07:30 AM
  3. Create formula for detecting duplicates
    By Courtneyf04 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 06:05 AM
  4. Create formula for detecting duplicates
    By Courtneyf04 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  5. [SOLVED] Create formula for detecting duplicates
    By Courtneyf04 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-08-2005, 11:05 AM

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