+ Reply to Thread
Results 1 to 11 of 11

Removing Items from a Column A based on Same Values in Column B, while simutanously..

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Removing Items from a Column A based on Same Values in Column B, while simutanously..

    Hi, So I have 3 Columns in an Excel Spread Sheet
    Column A contains a list of values
    Column B contains a list of values that may or may not be present in Column A
    Column C has equal number of values as column A and the values correspond to each other

    Now I need to remove ALL values from Column A that are in Column B

    Ex.
    Column A has
    A
    B
    C
    D
    E
    F
    G

    Column B has
    B
    C

    Column C has
    1
    2
    3
    4
    5
    6
    7


    the resultant is Column A
    A
    D
    E
    F
    G

    Column B
    B
    C

    Column C
    1
    2
    3
    4
    5
    6
    7


    this is done through this macro:

    Sub TryMe()
        Dim rRangeA As Range
        Dim rRangeB As Range
        Dim rCell As Range
         
        Set rRangeA = Range("A1", Range("A65536").End(xlUp))
        Set rRangeB = Range("B1", Range("B65536").End(xlUp))
         
        For Each rCell In rRangeA
            If WorksheetFunction.CountIf(rRangeB, rCell) > 0 Then
                rCell.Delete
            End If
        Next rCell
         
    End Sub
    Now what i want to do is have this macro simutanously delete values from Column C as well

    This means Column C should be
    1
    4
    5
    6
    7



    Any help is appreciated, im new to this, using Excel 2003.
    Last edited by AriX3; 10-21-2011 at 09:25 AM.

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Help with this macro

    Please see forum rules about enclosing your code in tags so it displays properly. [ CODE ] your code here [ /CODE ]. Here's a tweak to your code:
    Sub TryMe()
    Dim rRangeA As Range
    Dim rRangeB As Range
    Dim rCell As Range
    
    Set rRangeA = Range("A1", Range("A65536").End(xlUp))
    Set rRangeB = Range("B1", Range("B65536").End(xlUp))
    
    For Each rCell In rRangeA
         If WorksheetFunction.CountIf(rRangeB, rCell) > 0 Then
              rCell.Delete
              rCell.Offset(0,1).Delete
         End If
    Next rCell
    
    End Sub
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Help with this macro

    Quote Originally Posted by tlafferty View Post
    Please see forum rules about enclosing your code in tags so it displays properly. [ CODE ] your code here [ /CODE ]. Here's a tweak to your code:
    Sub TryMe()
    Dim rRangeA As Range
    Dim rRangeB As Range
    Dim rCell As Range
    
    Set rRangeA = Range("A1", Range("A65536").End(xlUp))
    Set rRangeB = Range("B1", Range("B65536").End(xlUp))
    
    For Each rCell In rRangeA
         If WorksheetFunction.CountIf(rRangeB, rCell) > 0 Then
              rCell.Delete
              rCell.Offset(0,1).Delete
         End If
    Next rCell
    
    End Sub
    thanks, ill edit my inital post.

    i tried what you wrote, it gave me runtime error 424?

  4. #4
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Help with this macro

    try switching these two lines:
              rCell.Offset(0,1).Delete
              rCell.Delete
    My guess is that after deleting rCell, Excel loses track of how to perform the offset.

  5. #5
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Help with this macro

    i added

    rCell.Offset(0,2).Delete
    rCell.Delete
    and it seems to work, however it is deleteing some entries in Column B (this isint an issue, but strange?). Now the entry in column C deletes at the same time with Column A, just confusing as to why some entries in Column B are deleted????

  6. #6
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Help with this macro

    Wait no, its actually MOVING some values from Column C to Column B. this is weird.

  7. #7
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Help with this macro

    I tested with this

    Test 1:
    Col A	Col B	Col C
    A	A	1
    B	E	2
    C	G	3
    D		4
    E		5
    F		6
    G		7
    
    Output
    Col A	Col B	Col C
    B	A	2
    C	E	3
    D	G	4
    F		6
    As expected, works.

    Test 2:
    Col A	Col B	Col C
    A	A	1
    B	B	2
    C	C	3
    D		4
    E		5
    F		6
    G		7
    
    Output:
    Col A	Col B	Col C
    B	A	2
    D	B	4
    E	C	5
    F		6
    G		7
    Does not work. Entry B should be removed. WHY?

    In my real data set, even weirder things are happened, entries in column C are being added to column B some how.....
    Last edited by AriX3; 10-20-2011 at 02:16 PM.

  8. #8
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Removing Items from a Column A based on Same Values in Column B, while simutanous

    If anyone has another function that can do this, ill would really appreciate it!

  9. #9
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Removing Items from a Column A based on Same Values in Column B, while simutanous

    It may be useful to add stops to your code at strategic points, then watch as the code executes. Below, I've added stops at some crucial points. Click the restore window button and size your visual basic editing window so that you can watch the code as it executes.
    Sub TryMe()
    Dim rRangeA As Range
    Dim rRangeB As Range
    Dim rCell As Range
    
    Set rRangeA = Range("A1", Range("A65536").End(xlUp))
    Set rRangeB = Range("B1", Range("B65536").End(xlUp))
    
    For Each rCell In rRangeA
         If WorksheetFunction.CountIf(rRangeB, rCell) > 0 Then
              Stop
              rCell.Delete
              Stop
              rCell.Offset(0,1).Delete
         End If
    Next rCell
    
    End Sub
    See if this helps isolate what's happening.

  10. #10
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Removing Items from a Column A based on Same Values in Column B, while simutanous

    nope didnt work.... highlights the stops in yellow...

  11. #11
    Registered User
    Join Date
    10-20-2011
    Location
    Mississauga
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Removing Items from a Column A based on Same Values in Column B, while simutanous

    I changed the code, works fine now. used a do until loop. thanks for the help!

+ 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