Results 1 to 9 of 9

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

Threaded 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

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