+ Reply to Thread
Results 1 to 8 of 8

VBA: find last cell with criteria

  1. #1
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    VBA: find last cell with criteria

    Hi,

    I've got the following find code:

    Please Login or Register  to view this content.
    Sheet1 has customer data, where each row corresponds to an individual. The first column is customerID number and the remaining columns have various customer details. Column 10 contains a date the customer details were valid till.

    My problem is, I've noticed that column1 has duplicate numbers due to updated customer details. The find command usually returns the value in the first cell with the stated value (value in sheet2!N1), but I want it to return the last cell which contains that value. Is there a way to tweak the code so that it returns the last cell instead?

    Alternatively, the last cell with the value in sheet2!N1 will always have a blank cell (no date) in the same row in column 10. So would it be possible for the find command to find the value of the cell in column 1 which has the value of sheet2!N1 and has a blank cell in the same row in column 10?

    Thanks in advance!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VBA: find last cell with criteria

    Can you post a sample workbook so we can see the layout?

    Regards

  3. #3
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: VBA: find last cell with criteria

    I've attached a workbook with sample data. Please let me know if you don't know what I'm getting at =)
    Sorry, noticed I didn't change the layout of sheet 2. I've attached a new workbook.

    Many thanks.
    Attached Files Attached Files
    Last edited by icylemontea; 09-14-2010 at 05:03 PM.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,428

    Re: VBA: find last cell with criteria

    I'm a little confused. Have you actually tested this sample workbook?

    There are two columns (A and B) called Customer ID on the second sheet which you have called "Pending" but which you refer to, in your code above, as sheet2. There's no code actually in the workbook.

    Copying the code above, renaming the worksheet, putting a value into cell N1 and then running the macro puts the First Name into the second Customer ID field.

    There's no error handling in the extract of code you have provided so, if the Customer ID isn't found, it will crash.

    Are you looking for someone to fix the code first before enhancing it?

    Regards

  5. #5
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: VBA: find last cell with criteria

    Sorry about that. It's because this was just sample data I created, so there were a few typos.
    I've amended the workbook above. Since I don't have the actual workbook with me at the moment, I had to do things from memory. The code I provided is only a small part of the full thing. Ultimately, I want to find and return all the fields in each column of the individual with the user ID in cell sheet2 N2.

    If you look at sheet 1 there are two rows with customerIDs 1230. The second row contains the updated data from 20/01/2004 and it is the fields in this row I want the 'Find' procedure to return, but the code I have provided returns the fields in the first row.

    I hope it's clearer now.
    Thanks

  6. #6
    Forum Expert contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2013
    Posts
    1,430

    Re: VBA: find last cell with criteria

    Hi. Is that what you want???
    Used formula

    =IF(COUNTIF(Sheet1!$A$2:$A$40,$N$1)<ROWS($A$1:$A1),"",INDEX(Sheet1!A$2:A$40,SMALL(IF($N$1=Sheet1!$A$2:$A$40,ROW(INDIRECT("1:"&ROWS(Sheet1!$A$2:$A$40)))),ROW(A1))))

    confirmed with Ctrl+Shift+Enter, then copied down and right.
    Attached Files Attached Files
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, ?Born in USSR?
    Vusal M Dadashev

    Baku, Azerbaijan

  7. #7
    Registered User
    Join Date
    09-03-2010
    Location
    england
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: VBA: find last cell with criteria

    thanks contaminated.
    Is there any way to get it using vba? It would be great if it can be done by tweaking the find command. It's only because this is only a small step of a really lengthy vba procedure. I need this to be done automatically when a user clicks a button since the amount of data I have is over 500 rows
    Last edited by icylemontea; 09-14-2010 at 06:06 PM.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA: find last cell with criteria

    Hello icylemontea,

    This macro is probably closer to what you need. This will find the last recurring customer id and copy the row to Sheet2 row 3. I added a button on Sheet2 to run the macro. This macro has been added to Module1 in the attached workbook.

    I could have expanded the Find method to check for recurring entries. This method of using a Dictionary object is both faster and more flexible, despite the additional code.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    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!)

+ 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