Results 1 to 3 of 3

Thread: Overstepping the Error?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    3

    Exclamation Overstepping the Error?

    I have written this code to filter through an array, however can't seem to pinpoint why when it goes to the Error it just bypasses the IF statement.

    For v = 0 To CInt(wbRef2.Sheets(1).UsedRange.Columns.Count) - 4 Step 1
    Dim stepArray
    stepArray = Array(1, 3, 5, 7)
    ArrayCheck = Application.Match(v, stepArray)
    On Error Resume Next

    '''This gets ignored
    If Err.Number > 0 Then
    wbRef2.Sheets(1).Range("H3").Offset(0, t) = customarray(t + 1)(d + 1)
    t = t + 1
    End If
    Next v
    Effectively it never carries out the command in wbRef.2...

    Thank you for your help guys, you have been amazing so far

  2. #2
    New Member
    Join Date
    Jul 2011
    Posts
    9

    Re: Overstepping the Error?

    The problem is that the purpose of
    Code:
    On Error Resume Next
    is to ignore the error and continue. It does this by setting Err.Number = 0.


    You need to use On Error Goto
    Like this
    Code:
    Sub MySub
    For v = 0 To CInt(wbRef2.Sheets(1).UsedRange.Columns.Count) - 4 Step 1
    Dim stepArray
    stepArray = Array(1, 3, 5, 7)
    ArrayCheck = Application.Match(v, stepArray)
    On Error goto ErrorHandler
    End If
    Next v
    '
    'Rest Of your code
    ' 
    Exit Sub
    ErrorHandler:
    If Err.Number > 0 Then
    wbRef2.Sheets(1).Range("H3").Offset(0, t) = customarray(t + 1)(d + 1)
    t = t + 1
    End If
    Resume Next
    End Sub

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Overstepping the Error?

    unless you want to change you error handling within the loop, you should place any on error statement prior to the loop
    similarly if steparray remains constant it too should be declared and assigned values prior to the loop
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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