+ Reply to Thread
Results 1 to 6 of 6

Difficulties with loops

Hybrid View

  1. #1
    Registered User
    Join Date
    03-31-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    7

    Difficulties with loops

    Hi,

    I have two columns: one with IDs and one with the number of Unique ID (0 or 1). I would like to create a macro that checks all the IDs, find the duplicates and sum the number of Unique ID for these duplicates. If the sum is different thant 1, I want the Unique ID for the first duplicate to be 1 and for the others duplicates to be 0. If there are no duplicates, the unique ID for the ID should be 1.

    For the moment the code will do the following:

    APID Unique ID

    ID 1 1
    ID 1 1
    ID 1 1
    ID 1 0
    ID 2 1
    ID 3 1
    ID 4 1
    ID 4 1
    ID 4 0

    And I want it to do:

    APID Unique ID

    ID 1 1
    ID 1 0
    ID 1 0
    ID 1 0
    ID 2 1
    ID 3 1
    ID 4 1
    ID 4 0
    ID 4 0

    Any clue? Thanks for your help

    
    Sub findDuplicates()
    
    Dim i As Integer
    Dim j As Integer
    Dim APID As Integer
    Dim UniqueAP As Integer
    
    Dim Lastrow
    Lastrow = Sheets("Open APs").Cells(Rows.Count, 1).End(xlUp).Row
    APID = 38
    UniqueAP = 39
    
    j = 2
    For i = 5 To Lastrow
        If Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + 1, APID).Value And Sheets("Open APs").Cells(i, UniqueAP).Value = 0 _
            And Sheets("Open APs").Cells(i + 1, UniqueAP).Value = 0 Then
            Sheets("Open APs").Cells(i, UniqueAP).Value = 1
        ElseIf Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + 1, APID).Value And Sheets("Open APs").Cells(i, UniqueAP).Value = 1 _
            And Sheets("Open APs").Cells(i + 1, UniqueAP).Value = 1 Then
            Sheets("Open APs").Cells(i + 1, UniqueAP).Value = 0
        ElseIf Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + 1, APID).Value And _
            Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + j, APID).Value Then
            j = j + 1
            If Application.Sum(Range(Cells(i, UniqueAP), Cells(j, UniqueAP))) <> 1 Then
                Sheets("Open APs").Cells(i, UniqueAP).Value = 1
            End If
        End If
    Next i
    
    End Sub
    Last edited by Minh92; 04-08-2015 at 10:39 AM.

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Difficulties with loops

    How about this:

    Sub excelforum()
    Dim lr&, x&, cell As Range
    
    lr = Cells(Rows.Count, 38).End(xlUp).Row
    Range(Cells(4, 38), Cells(lr, 38)).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Cells(1, Columns.Count), Unique:=True
    Range(Cells(5, 39), Cells(lr, 39)).Value = 0
    x = Cells(Rows.Count, Columns.Count).End(xlUp).Row
    
    For Each cell In Range(Cells(2, Columns.Count), Cells(x, Columns.Count))
    Cells(WorksheetFunction.Match(cell, Columns(4), 0), 39) = 1
    Next cell
    
    Columns(Columns.Count).Delete
    
    End Sub
    Please click the * below if this helps

  3. #3
    Registered User
    Join Date
    03-31-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    7

    Re: Difficulties with loops

    Hi,

    Thank you for your help but I don't really understand the logic behind this. I don't understand what is the use of x and columns.count, could explain in few words what is the logic?

    Thanks a lot

  4. #4
    Registered User
    Join Date
    03-31-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    7

    Re: Difficulties with loops

    Basically, I want the first i value = 1 and the others i to equal 0 without getting out of my loop. I just don't know how I could write it in code...

  5. #5
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Difficulties with loops

    My code replaces your whole loop.

    What it does is this:
    * It finds all the unique values in your 'duplicate range' and puts them in a temporary column.
    * It places a 0 next to all IDs
    * It loops through the unique values and places a 1 next to each first value it finds

    Should be what you're looking for and a lot quicker too.

    Example
    STEP 1: There's a bunch of IDs in your column 38 like below.
    ID1
    ID2
    ID1
    ID2
    ID3
    ID3
    ID4
    ID1
    ID5
    ID4

    STEP 2: a list is made with all unique IDs in that column
    ID1
    ID2
    ID3
    ID4
    ID5

    STEP 3: a 0 is placed next to all IDs in your range
    ID1 0
    ID2 0
    ID1 0
    ID2 0
    ID3 0
    ID3 0
    ID4 0
    ID1 0
    ID5 0
    ID4 0

    STEP 4: for each first occurrence of the ID in your range, the 0 is replaced by a 1
    ID1 1
    ID2 1
    ID1 0
    ID2 0
    ID3 1
    ID3 0
    ID4 1
    ID1 0
    ID5 1
    ID4 0

    As far as I understood, that's what you wanted, correct?

  6. #6
    Registered User
    Join Date
    03-31-2015
    Location
    London
    MS-Off Ver
    2010
    Posts
    7

    Re: Difficulties with loops

    This is definitely a better and faster solution than mine. Thank for your explanation.

    I am also highly interested in knowing why my solution did not work if you have a clue. This shows that I don't understand loopings and I need to improve my looping skills. Thanks

+ 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. Having some difficulties here.
    By rlvmaiden in forum Excel General
    Replies: 1
    Last Post: 05-23-2012, 01:48 PM
  2. Difficulties
    By Guest001 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-05-2008, 07:39 AM
  3. difficulties
    By ernie in forum Excel General
    Replies: 1
    Last Post: 02-13-2006, 04:45 PM
  4. Having difficulties with For If Next loops
    By havocdragon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2005, 02:05 PM

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