+ Reply to Thread
Results 1 to 6 of 6

Macro is not removing Duplicates as expected

Hybrid View

  1. #1
    Registered User
    Join Date
    02-05-2011
    Location
    Houston Texas
    MS-Off Ver
    Excel 2003
    Posts
    3

    Macro is not removing Duplicates as expected

    This macro should remove duplicates but it is not doing so.. Any ideas?

    Function SheetExists(SheetName As String) As Boolean
    ' returns TRUE if the sheet exists in the active workbook
        SheetExists = False
        On Error GoTo NoSuchSheet
        If Len(Sheets(SheetName).Name) > 0 Then
            SheetExists = True
        End If
    NoSuchSheet:
    
    End Function
    Sub GetOutput()
        
    ' Add an output sheet
        Sheets("Sheet1").Select
        If Not SheetExists("Output") Then
            Sheets.Add.Name = "Output"
        End If
        Sheets("Output").Select
    ' Add Headers
        Cells(1, "A").Value = "id_customer"
        Cells(1, "B").Value = "appointment_date"
        Cells(1, "C").Value = "appointment_time"
        Cells(1, "D").Value = "first"
        Cells(1, "E").Value = "last"
        Cells(1, "F").Value = "phone1"
        Cells(1, "G").Value = "cellphone"
        Cells(1, "H").Value = "pay_type"
        Cells(1, "I").Value = "treatment_type"
    ' Format columns
        Columns("C:C").Select
        
        Selection.NumberFormat = "hh:mm:ss" '"[$-F400]h:mm:ss AM/PM"
    
    ' Get the Date
        Sheets("Sheet1").Select
        Rownum = 3
        Offset = 3
        While Cells(Rownum, "A").Value = ""
            Rownum = Rownum + 1
        Wend
        lastRow = Rownum - 1
        nd = Len(Cells(Rownum, "A").Value) + 1
        bg = InStr(Cells(Rownum, "A").Value, "for") + 4
        Appointmentdate = Mid(Cells(Rownum, "A"), bg, nd - bg)
    ' For all appointments
        For Rownum = 5 To lastRow
        Sheets("Sheet1").Select
        'skip duplicates
        If Cells(Rownum, "C").Value > " " Then
            apttime = Cells(Rownum, "C").Value
        End If
            If Cells(Rownum, "D").Value <> Cells(Rownum - 1, "D") Then
         'get the time if its changed... if not then its the previous lines time
                 If Cells(Rownum, "D").Value > " " Then
     
                     bg = 1
                     nd = InStr(Cells(Rownum, "D").Value, ",")
                     lastname = Mid(Cells(Rownum, "D").Value, bg, nd - bg)
                     bg = nd + 1
                     nd = InStr(Cells(Rownum, "D").Value, "[")
                     firstname = Mid(Cells(Rownum, "D").Value, bg, nd - bg)
                     bg = nd + 1
                     nd = Len(Cells(Rownum, "D").Value)
                     custid = Mid(Cells(Rownum, "D").Value, bg, nd - bg)
                    'fill values
                     Sheets("Output").Cells(Rownum - Offset, "A").Value = custid
                     Sheets("Output").Cells(Rownum - Offset, "B").Value = Appointmentdate
                     Sheets("Output").Cells(Rownum - Offset, "C").Value = apttime
                     Sheets("Output").Cells(Rownum - Offset, "D").Value = firstname
                     Sheets("Output").Cells(Rownum - Offset, "E").Value = lastname
                     Sheets("Output").Cells(Rownum - Offset, "F").Value = Sheets("Sheet1").Cells(Rownum, "E").Value
                     cellphone = Replace(Sheets("Sheet1").Cells(Rownum, "F").Value, "-", "")
                     cellphone = "1" & cellphone
                     cellphone = Replace(cellphone, "(", "")
                     cellphone = Replace(cellphone, ")", "")
                     cellphone = Replace(cellphone, " ", "")
                     If cellphone <> "1" Then Sheets("Output").Cells(Rownum - Offset, "G").Value = cellphone
                     Sheets("Output").Cells(Rownum - Offset, "H").Value = Sheets("Sheet1").Cells(Rownum, "H").Value
                     Sheets("Output").Cells(Rownum - Offset, "I").Value = Sheets("Sheet1").Cells(Rownum, "I").Value
                Else:
                    Offset = Offset + 1
                End If
                    
            Else:
                    Offset = Offset + 1
            End If
    ' Next appointment until lastrow
         Next Rownum
         
        
    ' Save the active sheet as output file
        Sheets("Output").Select
    '    Dim i As Long, fname As Variant
    '    ActiveSheet.Copy
        'Do
        '    fname = Application.GetSaveAsFilename
        'Loop Until fname <> False
    '    ActiveWorkbook.SaveAs Filename:="output.csv", FileFormat:=xlCSV
    '    Workbooks(Workbooks.Count).Close
        
    'Delete output sheet
    '    Application.DisplayAlerts = False
    '    Sheets("Output").Delete
    '    Application.DisplayAlerts = True
    
    End Sub

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro is not removing Duplicates as expected

    Post a small sample workbook with the raw data, tell us exactly what you're evaluating, and perhaps a sample "output" file you expect as a result of the small sample data set.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-05-2011
    Location
    Houston Texas
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro is not removing Duplicates as expected

    Here are the before and after files. id_customer 2007 in column A , second record is not removed with the current macro and should be.

    The newest-original file is the macro file.
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Macro is not removing Duplicates as expected

    Hello christobalc,

    For the macro to work, you must first sort the data in ascending order using column "D".
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    02-05-2011
    Location
    Houston Texas
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Macro is not removing Duplicates as expected

    Can you pl
    ease post the correct code

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro is not removing Duplicates as expected

    see attachment, run macro "test"
    Attached Files Attached Files

+ 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