+ Reply to Thread
Results 1 to 4 of 4

This simple 10 line code doesn't work if just the reference is changed..why?

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    This simple 10 line code doesn't work if just the reference is changed..why?

    Awhile back, I asked for a macro to delete the whole row if a duplicate customer number was found in column B. Sometimes, though, my column numbers change. So, logically thinking, I simply changed the criteria, but the macro ONLY seems to work if duplicate customer numbers are in column B only.

    This code below won't work if the Customer Number is in column D instead of B even if the reference of B:B is changed to D:D, it doesn't carry the macro over...Why?


    Sub Delete_Dup_Customer_Numbers()
    r = 1
    Do
        Do While Application.WorksheetFunction.CountIf(ActiveSheet.Range("B:B"), Cells(r, 2)) > 1
             ActiveSheet.Range("B:B").Find(Cells(r, 2), Cells(r, 2)).EntireRow.Delete
        Loop
        r = r + 1
    Loop Until Cells(r, 2) = ""
    End Sub
    Thanks
    Last edited by duugg; 06-10-2009 at 02:29 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: This simple 10 line code doesn't work if just the reference is changed..why?

    Maybe like this (untested).
    Sub Delete_Dup_Customer_Numbers()
        Dim iRow As Long
        
        With ActiveSheet.Columns("D")
            Do
                Do While WorksheetFunction.CountIf(.Cells, .Cells(iRow).Value) > 1
                    .Find(.Cells(iRow).Value).EntireRow.Delete
                Loop
                iRow = iRow + 1
            Loop Until IsEmpty(.Cells(iRow).Value)
        End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

  3. #3
    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: This simple 10 line code doesn't work if just the reference is changed..why?

    Hello duugg,

    I am guessing you missed changing the Cells references in the code. They are looking at column 2 or "B". If you want to examine cells in column "D" then the column reference needs to be a 4.
    Sub Delete_Dup_Customer_Numbers()
    r = 1
    Do
        Do While Application.WorksheetFunction.CountIf(ActiveSheet.Range("B:B"), Cells(r, 2)) > 1
             ActiveSheet.Range("D:D").Find(Cells(r, 4), Cells(r, 4)).EntireRow.Delete
        Loop
        r = r + 1
    Loop Until Cells(r, 2) = ""
    End Sub
    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!)

  4. #4
    Valued Forum Contributor
    Join Date
    04-11-2006
    MS-Off Ver
    2007
    Posts
    438

    Re: This simple 10 line code doesn't work if just the reference is changed..why?

    Leith,

    Worked like a charm. I just have to remember to make 7 changes to the criteria anytime I would like to change the column reference.

    Thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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