+ Reply to Thread
Results 1 to 16 of 16

Execute code for first cell match

Hybrid View

kosherboy Execute code for first cell... 09-03-2014, 09:34 AM
AB33 Re: Execute code for first... 09-03-2014, 09:37 AM
kosherboy Re: Execute code for first... 09-03-2014, 09:40 AM
kosherboy Re: Execute code for first... 09-03-2014, 01:33 PM
AB33 Re: Execute code for first... 09-03-2014, 01:57 PM
kosherboy Re: Execute code for first... 09-03-2014, 02:21 PM
AB33 Re: Execute code for first... 09-03-2014, 02:25 PM
kosherboy Re: Execute code for first... 09-03-2014, 02:29 PM
AB33 Re: Execute code for first... 09-03-2014, 02:46 PM
kosherboy Re: Execute code for first... 09-03-2014, 02:59 PM
AB33 Re: Execute code for first... 09-03-2014, 03:10 PM
kosherboy Re: Execute code for first... 09-03-2014, 03:12 PM
AB33 Re: Execute code for first... 09-03-2014, 03:15 PM
kosherboy Re: Execute code for first... 09-03-2014, 03:27 PM
AB33 Re: Execute code for first... 09-03-2014, 03:31 PM
kosherboy Re: Execute code for first... 09-03-2014, 03:41 PM
  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.)

    Sub test()
    Dim rcell As Long
    Dim PFI As String
    Dim WS As Worksheet: Set WS = Sheets("Pro-Forma Invoice")
    PFI = WS.Range("G5")
    
    Windows("ORDERS 2014.xlsm").Activate
    With Sheets("Pending Orders")
     ' so right over here i would like to change the code to only do the action to the first match the code will find instead of repeating the code for every match.   
    For rcell = 5 To .Range("A" & Rows.Count).End(xlUp).Row
            If rcell Mod 2 = 1 Then
                If .Range("O" & rcell) = PFI Then
                .Range("O" & rcell).Select
                Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
                Range("A1").Value = "YOU DID IT!"
                End If
            End If
    Next rcell
    End With
    
    End Sub
    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:
    For rcell = 5 To .Range("A" & Rows.Count).End(xlUp).Row
    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
    .Range("O" & rcell).Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
    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:
    Sub test()
    Dim rcell As Long
    Dim PFI As String
    Dim WS As Worksheet: Set WS = Sheets("Pro-Forma Invoice")
    PFI = WS.Range("H7")
    
    Windows("ORDERS 2014.xlsm").Activate
    With Sheets("Pending Orders")
    
    For rcell = 5 To .Range("O" & Rows.Count).End(xlUp).Row
            If rcell Mod 2 = 1 Then
                If .Range("O" & rcell) = PFI Then
                .Range("O" & rcell).Select
                Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
                Range("A1").Value = "YOU DID IT!"
                End If
            End If
    
    Exit For
    
    Next rcell
    End With
    
    End Sub

  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.

    Option Explicit
    
    Sub test()
    Dim rcell As Long
    Dim PFI As String
    Dim rng As Range, FirstAddress As String
    Dim ws As Worksheet: Set ws = Sheets("Pro-Forma Invoice")
    PFI = ws.Range("H7")
    
    Application.ScreenUpdating = 0
    Windows("ORDERS 2014.xlsm").Activate
       With Sheets("Pending Orders").Columns("O")
          Set rng = .Find(PFI, .Cells(.Cells.Count), xlValues, xlWhole)
                If Not rng Is Nothing Then
                  FirstAddress = rng.Address
                   Do
                     .Cells(rng.Row, "O").Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
                     .Range("A1").Value = "YOU DID IT!"
                     Set rng = .FindNext(rng)
                  Loop While Not rng Is Nothing And rng.Address <> FirstAddress
                End If
       End With
    Application.ScreenUpdating = True
    End Sub

  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:
    Sub Macro2()
    
    Dim PFI As String
    Dim WS As Worksheet: Set WS = Sheets("Pro-Forma Invoice")
    PFI = WS.Range("H7")
    
    Windows("ORDERS 2014.xlsm").Activate
    With Sheets("Pending Orders")
    Cells.Find(What:=PFI, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    
    ActiveCell.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
    Range("A1").Value = "YOU DID IT!"
    End With
    
    End Sub
    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

    Cells.Find(What:=PFI, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    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:
    Option Explicit
    
    Sub test3()
    Dim rcell As Long
    Dim PFI As String
    Dim rng As Range, FirstAddress As String
    Dim ws As Worksheet: Set ws = Sheets("Pro-Forma Invoice")
    PFI = ws.Range("H7")
    
    Application.ScreenUpdating = 0
    Windows("ORDERS 2014.xlsm").Activate
       With Sheets("Pending Orders").Columns("O")
          Set rng = .Find(PFI, .Cells(.Cells.Count), xlValues, xlWhole)
                If Not rng Is Nothing Then
                  FirstAddress = rng.Address
                   Do
                     .Cells(rng.Row, "O").Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
                     .Range("A1").Value = "YOU DID IT!"
                     Set rng = .FindNext(rng)
                  Loop While Not rng Is Nothing And rng.Address <> FirstAddress
                End If
       End With
    Application.ScreenUpdating = True
    End Sub

  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

     rng.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True

  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
    .Range("A1").Value = "YOU DID IT!"
    to this
    ws.Range("A1").Value = "YOU DID IT!"
    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