+ Reply to Thread
Results 1 to 4 of 4

Thread: Compare Columns

  1. #1
    Registered User
    Join Date
    07-04-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Compare Columns

    Hi
    My first time here so hi to everyone

    I have two columns of adjacent data Call them A & B (although this could differ)

    If the data in the second colum is not equal to, blank cell or contains the words "CAT", "DOG" or "ELEPHANT" then the content of the cell in the second column should REPLACE the content of the adjacent cell in column A (in this example) leaving B blank, otherwise column B remains unchanged

    I wanting to run it from a form to enter the column references using a refedit (because the data as I said is not always in A & B it could be K & L for example) then a command button to run the VBA, .

    In other words,

    If column B contains anything other than , blank cell "CAT", "DOG" or "ELEPHANT" then the content of the cell in column B should REPLACE the content of the adjacent cell in column A leaving the cell in B blank, otherwise column B remains unchanged.

    If using the form or refedit is a problem then a standard macro I could run based on columns A & B would be most gratefull.

    Thanks

    Any help would be appreciated

  2. #2
    Valued Forum Contributor
    Join Date
    06-17-2009
    Location
    Chennai,India
    MS-Off Ver
    Excel 2003,excel 2007
    Posts
    394

    re: Compare Columns

    try the macro test
    there is an input box which will come up. type the column letter for e.g. B.
    the entries cat, dog etc should be exact without any other extra spaces etc.
    row 1 has column hadings
    the macro is

    Code:
    Sub test()
    Dim rng As Range, c As Range, col As String
    col = InputBox("type the column you are considering e.g. B")
    
    Worksheets("sheet1").Activate
    Set rng = Range(Range(col & 2), Cells(Rows.Count, col).End(xlUp))
    For Each c In rng
    'MsgBox c
    
    If c = "" Or c = "cat" Or c = "dog" Or c = "elephant" Then
    c.Cut
    c.Offset(0, -1).Select
    ActiveSheet.Paste
    End If
    Next c
    End Sub

  3. #3
    Registered User
    Join Date
    07-04-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Compare Columns

    Cheers for the reply Venkat1926


    sort of there

    I have attached a sample worksheet

    So it leaves anything that is in column B and is in the or statement in your code alone , but replaces the cell in column A with whatever is in column B

    Hope that makes sense

    Is there anyway one can
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    07-04-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    3

    Unhappy Re: Compare Columns

    Hi
    venkat1926 solution sort of works but the wrong way round.
    ie it replaces content of column A with the content of column B if it matches the references in the OR statement.

    I want it to replace the contents of column A if it does NOT match

    Can anyone help ?

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.2.0