+ Reply to Thread
Results 1 to 19 of 19

VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is found

  1. #1
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is found

    Hi
    Just started on VBA and I need to compare 2 cells on the same row (adjacent) in the two columns (column names: "Title" and "Author")

    The only real difference is if both cells contain the SAME letters then, for the Author column, I want to clear the contents where there is a match, then continue on to compare the next row and complete the same action.

    Note:Its similar in nature to mtt23's thread on VBA Code - Compare Adjacent Cells In two columns

    Sub hide_cs()
    Dim n As Long
    n = 17 ' Started from row 17
    Do While Rows(n).Columns("K").Value <> ""
    If InStr(ucase(Rows(n).Columns("K").Value), "C") > 0 And InStr(ucase(Rows(n).Columns("L").Value), "C") > 0 Then Rows(n).EntireRow.Hidden = True
    n = n + 1
    Loop

    End Sub

    I've attached a sample file. Any Suggestions welcome!
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Perhaps this.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Apologies for the delay in reply but...... it's not doing anything for me.
    I'm probably not putting this right but I use Title and Author as my ranges. I thought a straightforward substitution of range name would do the trick but clearly I dont know enough to pull it off!

    As i mentioned, VBA ain't my happy place

  4. #4
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    The above doesnt work for me, anyone else wanna give it a go?

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Where/how did you substitute 'range name'?

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,519

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Norie's code works for me on your uploaded example workbook.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  7. #7
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Hi Guys

    Thanks for taking a look. Your comments made me go back and look at the code again. Good news is it works when I use column J and K as my ranges. Bad news is I can’t get it to do so using my named ranges. I get the feeling I’m missing the fundamentals here…

    Just as background, I have an .csv output file that from time to time will have a different number of columns. I’ve deliberately avoided referring to column J, K etc. as only columns with info will be kept i.e. J, K may not necessarily be Title and Author. Instead of using column letters J, K I’ve used column headers to ID the relevant columns. Using the column headers I then define the range…or at least that’s what I think I’m doing.

    Please note:
    This is part of a form driven macro- the form prompts the user to find said .csv then apply all the changes to the file.
    The sheet name is always a series of numbers, I rename the sheet to Sheet1 using ActiveSheet.Name = "Sheet1"
    There a number of other changes I make along the way which I’ve obviously excluded.


    Ok, so noob me did the following……..

    Please Login or Register  to view this content.
    Any assistance would be appreciated.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    What are the named ranges called?

    Are you trying to refer to them with code like this
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Yes I am, the fact that the columns can shrink means I cant take the easy route

  10. #10
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    I mean number of columns can shrink

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    So what are they called?

  12. #12
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    err... dont i do that when i say

    Please Login or Register  to view this content.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    That isn't a named range, it's a variable referring to a range.

    I was going to post code for named ranges.

  14. #14
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    I think this may be what you are looking for

    Please Login or Register  to view this content.
    the columns holding the title and author are identified by looking at the textual values held on row 1, so basically the column numbers are determined dynamically.
    If you like my contribution click the star icon!

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Try this.
    Please Login or Register  to view this content.

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    @Norie,

    I am learning something new every day. Never used the Match possibility myself

    @Langer101,

    So

    Please Login or Register  to view this content.
    could be replaced with

    Please Login or Register  to view this content.
    Although it would probably be wise to add some error checking to make sure the columns are actually found

  17. #17
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Norie, it worked like a charm! Thanks every much.

    Last question if you dont mind. I like to know HOW to get to the solution not just the solution itself (though it's appreciated!!)

    When you mentioned "That isn't a named range,......." I assumed that if I added a range name it would sort out my problem. With the code that I have how should i have stated it or was i waaaaay off base? I suspect a lot of my problems is in not identifying my ranges properly.

    Please Login or Register  to view this content.
    Olaf, regarding "column numbers are determined dynamically" as noob I can actually follow your code with SOME understanding This might help me solve some problems I've had with another project. Thank you

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Not sure if adding a named range would help.

    It wouldn't do any harm though and might be useful elsewhere.

    In that code the way you are identifying the range is fine, could do with a worksheet reference though.

    The code could be tidied up a bit I suppose.
    Please Login or Register  to view this content.
    Last edited by Norie; 01-03-2013 at 06:23 AM.

  19. #19
    Registered User
    Join Date
    12-04-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    34

    Re: VBA Code - Compare Adjacent Cells In two columns and clear contents when a match is fo

    Norrie, Olaf Big thumbs up!

+ 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