+ Reply to Thread
Results 1 to 4 of 4

Stop code from running when I click "Cancel"

  1. #1
    Dean
    Guest

    Stop code from running when I click "Cancel"

    I have a small issue with this code. While running the code the "Input
    box" appears and when I click cancel the code still excecutes and
    copies everything on the "database" page to the "found page".

    I would value some ideas on how to stop the code from executing when I
    click "cancel" on the Input box.

    Thanks,
    Dean


    Sub Macro2()
    Dim LastRow As Long, MyCriteria, _
    rCriteriaField As Range, rPointer As Range, rCopyTo As Range

    ' This variable has the value of the criteria by which you intend
    ' to select records to extract. Lets assume you are evaluating
    ' the entries in column A of your source table. This can be either
    ' text or numeric.
    MyCriteria = InputBox("Enter Dept Code")

    ' Initialize a variable for the last possible record in a worksheet
    If Left(Application.Version, 1) < 8 Then _
    LastRow = 5700 Else LastRow = 65536

    With ThisWorkbook

    ' Initialize a range object variable for the entire populated
    ' area of column B (excluding row 1 for a header)
    With Worksheets("database")
    Set rCriteriaField = .Range(.Cells(2, 2), _
    .Cells(Application.Max(2, _
    .Cells(LastRow, 1).End(xlUp).Row), 1))
    End With

    Set rCopyTo = .Worksheets("found").Cells(2, 1)
    End With

    ' Loop through all the records in your source data table
    For Each rPointer In rCriteriaField
    With rPointer

    ' If there is a match on the criteria in col A then copy
    ' the record to the destination table
    If InStr(1, .Value, MyCriteria) > 0 Then
    .Resize(, 8).Copy
    rCopyTo.PasteSpecial xlPasteValues

    ' Advance the pointer in your destination table to the
    ' next available row
    Set rCopyTo = rCopyTo.Offset(1, 0)
    End If
    End With
    Next rPointer
    End Sub


  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486
    the code in the cancel button should read
    exit sub

  3. #3
    Haldun Alay
    Guest

    Re: Stop code from running when I click "Cancel"

    after the line

    MyCriteria = InputBox("Enter Dept Code")

    add this

    If MyCriteria = "" Then Exit Sub

    after adding this line, when you execute the code and press cancel button (or press OK button while the inputbox is empty) , it will stop the execution of rest of the code.

    --
    Haldun Alay
    "Dean" <[email protected]>, haber iletisinde sunlari yazdi:[email protected]...
    I have a small issue with this code. While running the code the "Input
    box" appears and when I click cancel the code still excecutes and
    copies everything on the "database" page to the "found page".

    I would value some ideas on how to stop the code from executing when I
    click "cancel" on the Input box.

    Thanks,
    Dean


    Sub Macro2()
    Dim LastRow As Long, MyCriteria, _
    rCriteriaField As Range, rPointer As Range, rCopyTo As Range

    ' This variable has the value of the criteria by which you intend
    ' to select records to extract. Lets assume you are evaluating
    ' the entries in column A of your source table. This can be either
    ' text or numeric.
    MyCriteria = InputBox("Enter Dept Code")

    ' Initialize a variable for the last possible record in a worksheet
    If Left(Application.Version, 1) < 8 Then _
    LastRow = 5700 Else LastRow = 65536

    With ThisWorkbook

    ' Initialize a range object variable for the entire populated
    ' area of column B (excluding row 1 for a header)
    With Worksheets("database")
    Set rCriteriaField = .Range(.Cells(2, 2), _
    .Cells(Application.Max(2, _
    .Cells(LastRow, 1).End(xlUp).Row), 1))
    End With

    Set rCopyTo = .Worksheets("found").Cells(2, 1)
    End With

    ' Loop through all the records in your source data table
    For Each rPointer In rCriteriaField
    With rPointer

    ' If there is a match on the criteria in col A then copy
    ' the record to the destination table
    If InStr(1, .Value, MyCriteria) > 0 Then
    .Resize(, 8).Copy
    rCopyTo.PasteSpecial xlPasteValues

    ' Advance the pointer in your destination table to the
    ' next available row
    Set rCopyTo = rCopyTo.Offset(1, 0)
    End If
    End With
    Next rPointer
    End Sub


  4. #4
    Dean
    Guest

    Re: Stop code from running when I click "Cancel"

    Thanks Gents. That did the trick.


+ 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