Results 1 to 10 of 10

Thread: [RESOLVED] How to Continue Error Trapping?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    155

    Resolved [RESOLVED] How to Continue Error Trapping?

    How do I get my error trapping to continue after it has caused an error. What happens is the error occurs, is goes to the correct trap, continues running but when there is another error, it does not trap it, it just gives the msgbox with the error. Here is the code.

    VB Code:
    1. Sub CheckMFGPartInSAP()
    2.  
    3. Dim MFGNo, ZE, Sta, Item
    4. Dim SAPBook, LimaBook, CheckRow
    5. LimaBook = "Copy of D35 ENGINE ASSEMBLY SPARE PARTS MASTER MATRIX.xls"
    6. SAPBook = "SAP Dump.xls"
    7.  
    8. Do Until ActiveCell = ""
    9.     MFGNo = ActiveCell
    10.     Item = ActiveCell.Row
    11.     Workbooks(SAPBook).Activate
    12.     On Error GoTo NotFound
    13.     Range("K4:K3534").Find(MFGNo, , , , , xlNext).Activate
    14.     CheckRow = ActiveCell.Row
    15.     If Range("A" & ActiveCell.Row) = "" Then
    16.         Range("A" & ActiveCell.Row) = Item
    17.     Else
    18.         Range("A" & ActiveCell.Row) = Range("A" & ActiveCell.Row) & "," & Item
    19.     End If
    20.     Range("K4:K3534").Find(MFGNo, ActiveCell, , , , xlNext).Activate
    21.     Do Until ActiveCell.Row = CheckRow
    22.         If Range("A" & ActiveCell.Row) = "" Then
    23.             Range("A" & ActiveCell.Row) = Item
    24.         Else
    25.             Range("A" & ActiveCell.Row) = Range("A" & ActiveCell.Row) & "," & Item
    26.         End If
    27.         Range("K4:K3534").Find(MFGNo, ActiveCell, , , , xlNext).Activate
    28.     Loop
    29.  
    30. NotFound:
    31.     'MsgBox Err.Description & Chr(10) & Err.Number
    32.     Err.Clear
    33.     Workbooks(LimaBook).Activate
    34.     ActiveCell.Offset(1, 0).Activate
    35. Loop
    36.  
    37. End Sub

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to Continue Error Trapping?

    I think you must Resume or Resume Next to continue error trapping.

    Try This:
    VB Code:
    1. .
    2. .
    3. .
    4. NotFound:
    5.     'MsgBox Err.Description & Chr(10) & Err.Number
    6. [B]    If Err > 0 Then Resume NotFound[/B]
    7. '    Err.Clear
    8.     Workbooks(LimaBook).Activate
    9.     ActiveCell.Offset(1, 0).Activate
    10. Loop

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to Continue Error Trapping?

    Resume Next will begin executing the code on the line immediately following the line that generated the error.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How to Continue Error Trapping?

    Your error trap should not be in your loop:

    VB Code:
    1. Sub CheckMFGPartInSAP()
    2.  
    3. Dim MFGNo, ZE, Sta, Item
    4. Dim SAPBook, LimaBook, CheckRow
    5. LimaBook = "Copy of D35 ENGINE ASSEMBLY SPARE PARTS MASTER MATRIX.xls"
    6. SAPBook = "SAP Dump.xls"
    7.  
    8. On Error GoTo NotFound
    9.  
    10. Do Until ActiveCell = ""
    11.     MFGNo = ActiveCell
    12.     Item = ActiveCell.Row
    13.     Workbooks(SAPBook).Activate
    14.     Range("K4:K3534").Find(MFGNo, , , , , xlNext).Activate
    15.     CheckRow = ActiveCell.Row
    16.     If Range("A" & ActiveCell.Row) = "" Then
    17.         Range("A" & ActiveCell.Row) = Item
    18.     Else
    19.         Range("A" & ActiveCell.Row) = Range("A" & ActiveCell.Row) & "," & Item
    20.     End If
    21.     Range("K4:K3534").Find(MFGNo, ActiveCell, , , , xlNext).Activate
    22.     Do Until ActiveCell.Row = CheckRow
    23.         If Range("A" & ActiveCell.Row) = "" Then
    24.             Range("A" & ActiveCell.Row) = Item
    25.         Else
    26.             Range("A" & ActiveCell.Row) = Range("A" & ActiveCell.Row) & "," & Item
    27.         End If
    28.         Range("K4:K3534").Find(MFGNo, ActiveCell, , , , xlNext).Activate
    29.     Loop
    30.  
    31. BackIn:
    32.     Workbooks(LimaBook).Activate
    33.     ActiveCell.Offset(1, 0).Activate
    34. Loop
    35.  
    36. NotFound:
    37.     'MsgBox Err.Description & Chr(10) & Err.Number
    38.     Err.Clear
    39.     Resume BackIn
    40.  
    41. End Sub
    I wasn't sure if the Workbooks(Lima...) or ActiveCell... parts should be in the error trap or not...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How to Continue Error Trapping?

    I assume you are Error trapping for when the searched for value is not found?

    You don't need to trap for that at all as the Find method returns Nothing if the value is not found.
    Have a look at my posts here here to see how to trap for Nothing

    While I'm here, all your varaiables are declared as variants - not a good idea. If you had setup your workbooks as object variables, you could make this code much simpler.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    155

    Re: How to Continue Error Trapping?

    Resume to a specific location worked. Thanks

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    155

    Re: How to Continue Error Trapping?

    Quote Originally Posted by DKenny
    I assume you are Error trapping for when the searched for value is not found?

    You don't need to trap for that at all as the Find method returns Nothing if the value is not found.
    Have a look at my posts here here to see how to trap for Nothing

    While I'm here, all your varaiables are declared as variants - not a good idea. If you had setup your workbooks as object variables, you could make this code much simpler.
    Sorry DKenny but the Find method will not return nothing. If it does not find anything then it returns an error. Your post reference does not even use the Find method. I was in a hurry to write the macro and that is why I didn't define all my variables. I don't like having set all my objects to nothing if I forget about one.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: How to Continue Error Trapping?

    Per the VBA help file.
    "Finds specific information in a range, and returns a Range object that represents the first cell where that information is found. Returns Nothing if no match is found. Doesn’t affect the selection or the active cell."
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2004
    Posts
    155

    Re: How to Continue Error Trapping?

    Quote Originally Posted by DKenny
    Per the VBA help file.
    "Finds specific information in a range, and returns a Range object that represents the first cell where that information is found. Returns Nothing if no match is found. Doesn’t affect the selection or the active cell."
    Ok, sorry DKenny, I didn't see the previous code example from the reference post you submitted. I now see how it is set and checked. Thanks for the help!

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: [RESOLVED] How to Continue Error Trapping?

    This might also help.

    VB Code:
    1. Sub NewCheckMFGPartInSAP()
    2. Dim wkbLima As Workbook
    3. Dim wkbSAP As Workbook
    4. Dim rngChkCell As Range
    5. Dim rnMatchArea As Range
    6. Dim rngMatch As Range
    7. Dim lMFGNo As Long
    8. Dim rngItem As Range
    9.        
    10.     Set wkbLima = Workbooks("Copy of D35 ENGINE ASSEMBLY SPARE PARTS MASTER MATRIX")
    11.     Set wkbSAP = Workbooks("SAP Dump")
    12.    
    13.     Set rngChkCell = wkbLima.Worksheets(1).Range("A1")
    14.     Set rngMatcharea = wkbSAP.Worksheets(1).Range("K4:K3534")
    15.    
    16.     Do While rngChkCell.Value <> ""
    17.        
    18.         lMFGNo = rngChkCell.Value
    19.         Set rngMatch = rnMatchArea.Find(What:=lMFGNo, LookIn:=xlValues)
    20.        
    21.         If Not (rngMatch Is Nothing) Then
    22.             'rest of code here
    23.         End If
    24.     Loop
    25.    
    26.     Set rngMatch = Nothing
    27.     Set rngMatcharea = Nothing
    28.     Set rngChkCell = Nothing
    29.     Set wkbSAP = Nothing
    30.     Set wkbLima = Nothing
    31. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width