+ Reply to Thread
Results 1 to 20 of 20

Find code not working

  1. #1
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Find code not working

    Hi All,

    I was working on this code for a friend but am not making any headway.

    I have to compare the headers between 2 sheets. If sheet1 has a header which is not present in sheet2, i need to delete the column in sheet1.
    In other words, ensure that the headers match between both the sheets.

    I tried this code
    Please Login or Register  to view this content.
    When i didnt have the error handlers, i got the type mismatch error on the set scol=...line. I tried researching and checking on why the type mismatch error is occurring but couldnt.
    When i put in the error handlers, even if it finds the header in both the sheets, it deletes it which should not happen.

    Please help me.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find code not working

    Arlu try this.

    Code:

    Please Login or Register  to view this content.
    Last edited by JapanDave; 05-24-2012 at 06:20 AM.
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find code not working

    Its still not working Dave. The search value is found in both the sheets so it should ignore it and move to the next search value (next column header).

  4. #4
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find code not working

    I just realised you were trying to delete on sheet1,

    This will work.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find code not working

    No, still not working. If i remove the "On error resume next" and "On error goto 0", i still get the type mismatch error on the set scol = ....line.

  6. #6
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find code not working

    Ok, I tested it this time. Give this a shot.

    Please Login or Register  to view this content.
    Edit: Btw, if find does not find anything your will get an error which is why the error handler is needed. That is why the below code is like it is.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-24-2012
    Location
    Bombay
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Find code not working

    Hi,

    You need to replace the line
    If scol = "" Then
    with
    If scol Is Nothing Then

    It should work then

    Thanks
    Deepal

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find code not working

    Its still not working. Maybe i need to explain another thing. The first cell that is being searched contains a date ( for e.g. Nov 11 2011). THis date is present in both sheets, so it should ideally move to the next header in sheet1. But its not moving, instead it is deleting the first column.

  9. #9
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find code not working

    Arlu, if you can post a dummy workbook it would be helpful. I tested the last code and it works fine. I am guessing their is slight differences in the two cells being looked up.

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find code not working

    Here it is. Sheet 1 row 4 should be compared to sheet 2 row 2. And whatever header does not match, the entire column from sheet1 should be deleted.
    Attached Files Attached Files

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Find code not working

    I reckon probably:
    Please Login or Register  to view this content.
    Last edited by JosephP; 05-24-2012 at 08:47 AM. Reason: pasted over my comment
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find code not working

    Thanks Joseph, it works great (dont know why my find code didnt work), but its not recognizing numbers. For e.g its not recognizing $5000.

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Find code not working

    it does for me. did you use exactly my code or just copy some of it?

  14. #14
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find code not working

    Ok see if this works for you. I have tested this and It is working for me.

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find code not working

    Quote Originally Posted by JosephP View Post
    it does for me. did you use exactly my code or just copy some of it?
    I used your code completely, bit to bit.

  16. #16
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Find code not working

    It is not recognizing the number value due to your custom formatting. Change the format of the cell to general and see that it picks up the value.

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Find code not working

    here's your file with the code in - tried and tested. can you just confirm it still deletes the 5k column for you?
    Attached Files Attached Files

  18. #18
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Find code not working

    if it still fails, you might replace
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.

  19. #19
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Find code not working

    Great it works now. It didnt take the 5000 becoz of the currency formatting.

    Thanks to both of you. The find code has never been a cake walk for me and this only affirms it more.

  20. #20
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Find code not working

    can you just confirm for me that the sample I posted still didn't work for you?

+ 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