Results 1 to 9 of 9

using err.raise works first time but subsequently produces and automation error

Threaded View

  1. #1
    Registered User
    Join Date
    06-10-2011
    Location
    Mansfield, England
    MS-Off Ver
    Excel 2007
    Posts
    10

    Question using err.raise works first time but subsequently produces and automation error

    Hi All,
    I have a generic function I use to validate if a value is within a drop down list. I recently decided to make it error within the function rather than passing it back to the calling proc, and I decided to do this by raising a custom error. This works as expected the first time it is invoked, but second and subsequent times produce an Automation error on the err.raise line. The complete sequence of events are:
    1. user presses "Validate" button to start validation code
    2. A user entered value fails the lookup and the expected error msgbox in the error handler is displayed
    3. Usr clicks OK in msgbox and validation ends
    4. User corrects the error (or not) and then again presses the validate button
    5. Either the same value or another value fails the lookup function, but this time a "Run-time error '-2147220503 (800403e9)' Automation error is thrown
    6. pressing debg takes me to the err.raise line
    Full procedure shown below - Any advice welcome
    Public Function DMLookup(LookupName As String, SearchValue As String, SearchCol As Integer, ResultCol As Integer, Optional IsCaseSensitive As Variant)
    Dim LoopCounter As Long
    Dim LookupFound As Boolean
    Dim LookupToBeUsed As Variant
    Dim result As String
        If IsMissing(IsCaseSensitive) Then
            IsCaseSensitive = False
        End If
        
        If gEnableErrorHandling Then On Error GoTo DMLookup_Error
            
        LookupFound = False
        LookupToBeUsed = Application.Names(LookupName).RefersToRange.Value
        For LoopCounter = 1 To UBound(LookupToBeUsed, SearchCol)
            'If its not a case sensitive check, wrap the check in UCASe to convert both values to upper case
            If IsCaseSensitive = False Then
                If UCase(LookupToBeUsed(LoopCounter, SearchCol)) = UCase(SearchValue) Then
                    result = LookupToBeUsed(LoopCounter, ResultCol)
                    LookupFound = True
                    Exit For
                End If
            Else
                'If it is a case sensitive search, do a direct compare of the values
                If LookupToBeUsed(LoopCounter, SearchCol) = SearchValue Then
                    result = LookupToBeUsed(LoopCounter, ResultCol)
                    LookupFound = True
                    Exit For
                End If
            End If
        Next LoopCounter
        If LookupFound = False Then
            Err.Raise (vbObjectError + 1001)
        End If
        DMLookup = result
    DMLookup_Exit:
        On Error GoTo 0
        Exit Function
    
    DMLookup_Error:
        If Application.ScreenUpdating = False Then Application.ScreenUpdating = True
        Select Case Err.Number
            Case vbObjectError + 1001
                MsgBox "The lookup for " & SearchValue & "withing the lookup name " & LookupName & _
                " has failed - This is often due to a copy and paste error where 1 lookup has been overwritten by another " & _
                " and so the validation stage passes initially, but is detected in the generation stage and so fails." & _
                vbCrLf & vbCrLf & "This is a critical error and any further processing will stop - Please correct the " & _
                "error before retrying", _
                vbCritical, "Error in function DMLookup of Module DeveloperToolKit"
            Case Else
                MsgBox "An unexpected error has occured, please contact Design with the below error details." & _
                vbCrLf & "Module = Module DeveloperToolKit" & _
                vbCrLf & "Procedure = DMLookup" & _
                vbCrLf & "Line = " & Erl & _
                vbCrLf & "Error Code = " & Str$(Err.Number) & _
                vbCrLf & "Error Text = " & Err.Description, vbCritical, "Error in in procedure DMLookup of Module DeveloperToolKit"
        End Select
        
        End 'Stop everything
    End Function
    Last edited by edwar368; 09-18-2014 at 10:34 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro for Replacing Text Produces Run-Time Error 91
    By bryanmarks in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2014, 10:22 AM
  2. Simple polynomial produces error ONLY for a certain input, otherwise works
    By anna86 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-15-2014, 01:57 PM
  3. [SOLVED] ComboList produces errors run-time error...
    By radgrad in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-13-2014, 09:24 PM
  4. [SOLVED] Excel produces Error when the code works
    By WadeLair in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-09-2012, 11:15 PM
  5. Clicking Red X on UserForm produces run-time error '91'
    By ChrisMattock in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2008, 04:38 PM

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