+ Reply to Thread
Results 1 to 9 of 9

How to exit a loop once the value is found in the range

Hybrid View

  1. #1
    Registered User
    Join Date
    11-14-2008
    Location
    Saint Lucia
    Posts
    14

    Exclamation How to exit a loop once the value is found in the range

    Hi Guys,

    The following vba code runs when the user clicks the command button. It first calculates the CurrentWk and then searches the range c for the resulting value. I want the macro to run through each cell in the range c and exit the loop once c.value = CurrentWk.

    Can anyone assist?

    Private Sub cmdForecast_Click()
    
    Dim CurrentWk As Integer
    Dim Age As Integer
    Dim r As Integer
    Dim c As Range
    
    'Go to 12-Week Forecast sheet
    Sheets("12-Week Forecast").Select
    
    ActiveSheet.Range("G3").Select
    
    Age = 13
    
    'Calculate Current Week
    CurrentWk = ActiveSheet.Range("G3").Value - Age
    
    'Get Final Row
    r = Cells(Rows.Count, 1).End(xlUp).Row
    
    
    Do
    For Each c In Range("A5:A" & r)
    
        If c.Value = CurrentWk Then
            'Select range going down 15 rows
            c.Select
        End If
    
    Next c
    Loop Until c.Value = CurrentWk
    
    
    End Sub
    Last edited by Suety; 09-28-2010 at 01:29 PM. Reason: Question answered

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: How to exit a loop once the value is found in the range

    Simplify
    Do
    For Each c In Range("A5:A" & r)
    
        If c.Value = CurrentWk Then
            'Select range going down 15 rows
            c.Select
        End If
    
    Next c
    Loop Until c.Value = CurrentWk
    to
    For Each c In Range("A5:A" & r)
    
        If c.Value = CurrentWk Then
            'Select range going down 15 rows
            c.Select
            Exit For
        End If
    
    Next c
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: How to exit a loop once the value is found in the range

    Or do it properly, without the need to select random stuff...

    Dim FindWk
    
    Set FindWk=Range("A5:A" & r).Find(CurrentWk, lookin:=xlValues, lookat:=xlWhole)
    
    If Not FindWk Is Nothing Then
      FindWk.Select
    End If
    Avoids the need to loop altogether.

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: How to exit a loop once the value is found in the range

    Quote Originally Posted by Andrew-R View Post
    Or do it properly, without the need to select random stuff...

    Dim FindWk
    
    Set FindWk=Range("A5:A" & r).Find(CurrentWk, lookin:=xlValues, lookat:=xlWhole)
    
    If Not FindWk Is Nothing Then
      FindWk.Select
    End If
    Avoids the need to loop altogether.
    And a lovely solution 'tis!

  5. #5
    Registered User
    Join Date
    11-14-2008
    Location
    Saint Lucia
    Posts
    14

    Re: How to exit a loop once the value is found in the range

    Thanks guys for the suggestions! They both solved my problem...

    I have another question. In my code, if c.value = CurrentWk, i want it to select c and select 15 rows going down. how do i do this?

    For Each c In Range("A5:A" & r)
    
        If c.Value = CurrentWk Then
            'Select range going down 15 rows
            c.Select
            Exit For
        End If
    
    Next c

  6. #6
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: How to exit a loop once the value is found in the range

    Liking Andrew's solution better, I would do

    Dim FindWk
    
    Set FindWk=Range("A5:A" & r).Find(CurrentWk, lookin:=xlValues, lookat:=xlWhole)
    
    If Not FindWk Is Nothing Then
      Range(FindWk.Offset(0, 0), FindWk.Offset(14, 0)).Select
    End If

  7. #7
    Registered User
    Join Date
    11-14-2008
    Location
    Saint Lucia
    Posts
    14

    Re: How to exit a loop once the value is found in the range

    Thanks Blane! i used this suggestion... i forgot to mention something in my previous post.. i need to select 15 rows down and column A:AS across.

  8. #8
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: How to exit a loop once the value is found in the range

    You can the offset parameters accordingly.

  9. #9
    Registered User
    Join Date
    11-14-2008
    Location
    Saint Lucia
    Posts
    14

    Re: How to exit a loop once the value is found in the range

    thanks guys for all your help! it was much appreciated...

+ 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