+ Reply to Thread
Results 1 to 16 of 16

Execute code for first cell match

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Execute code for first cell match

    Hello,
    It's been a while since I've posted anything, wondering if there is a different way to write following code. (I am trying to avoid the repeat scenario, i just want to focus on the first cell match even if there are duplicates.)

    Please Login or Register  to view this content.
    Thanks in advance

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Execute code for first cell match

    You can exit the loop once the code returns the first match.
    After end if
    exit sub or
    exit for

  3. #3
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    Wow! That did the trick! Thanks

  4. #4
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    Quote Originally Posted by AB33 View Post
    You can exit the loop once the code returns the first match.
    After end if
    exit sub or
    exit for
    Hi AB33,
    Little issue, this code works fine if the row where i'm taking action on is on the fifth line as stated in the code:
    Please Login or Register  to view this content.
    However, if the row that i'm taking action on is on the 7th, 9th, 11th....etc, the code won't run.
    Any ideas?

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Execute code for first cell match

    The code loops from row 5 to the bottom of column A. If the code finds the match in row 5, it exit the sub or loop. If the match is not found on 5, it goes to row 6, but the if if statement returns false on that row, then the code goes to next loop, row 7, if it finds match, then
    Please Login or Register  to view this content.
    is linked and the code exit the sub.
    In other words, the code creates the hyper link using the row rcell and column O and exit the sub.

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    Quote Originally Posted by AB33 View Post
    The code loops from row 5 to the bottom of.......
    I think i understand what you are saying but not 100% sure
    Here is the code which i modified but it only works on row 5 but not any rows after that:
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Execute code for first cell match

    Kosherboy,
    I might misunderstood your aim.
    If the code returns true to the if condition on row5, there is nothing to be done on the loop. The code exit the loop. Since you do not have any other lines to execute- except the loop, the code will exit too-Goes to end sub.

  8. #8
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    I hear. So is there any alternative solutions?

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Execute code for first cell match

    There may be, but what is the objective? What are trying to do once a match is found?

    You can try the find function.

  10. #10
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    i posted a random example but what i really want to do involves a long boring code.
    Can i record the find function?

  11. #11
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Execute code for first cell match

    This is not a complete code, but will give you a good starting point for find function. Since I do not know what is your aim for this code, I have included next find function. The code does not look at odd rows, but all rows.

    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    Thanks!
    i came up with this:
    Please Login or Register  to view this content.
    Can you briefly explain what your version is doing "extra"?
    Thanks!

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Execute code for first cell match

    Lots

    Please Login or Register  to view this content.
    need to be set as a range. your code will error if no match is found
    Your code is looking in all cells, not just column O.

  14. #14
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    Ah! I see

    OK, so ran your code but got an error message saying " Subscript out of range" for the highlighted part:
    Please Login or Register  to view this content.

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Execute code for first cell match

    I do not know why you are getting an error, try

    Please Login or Register  to view this content.

  16. #16
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Execute code for first cell match

    Yay! It worked!

    i just had to change this part
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    Thanks so much!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Automatic code to execute only if certain cell is empty
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-25-2014, 10:49 PM
  2. When cell changes, execute code
    By kirkland in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-15-2012, 09:06 AM
  3. macro to execute code from cell.value?
    By wamp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2010, 04:29 AM
  4. Execute code based on cell entry
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 10-29-2009, 01:38 PM
  5. How can I use VB code to execute macro when double-clicking cell?
    By JDay01 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-13-2006, 10:20 AM

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