+ Reply to Thread
Results 1 to 4 of 4

Proper Error Handling Routine

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Proper Error Handling Routine

    Hi all,

    I'm trying to get more familiar with Error Handling in VBA. I am using below code and need to set the value of my variables x and y to 0 every time an error occurs. What is the best way of doing that?

    Basically, if it cannot find the TextBox in my UserForm or if there's a divison error, it should set the variable y or x to 0, if there's no error, it should calculate/lookup the actual value and then resume to the last bit of my module where x and y are compared and the TextBox coloured accordingly.

    Sub HandleError()
    
        Dim i As Long
                
        For i = 1 To UBound(Evaluate(ActiveWorkbook.Names("L4_Values").RefersTo))
                
            Dim x As Long
            Dim y As Long
                    
            On Error GoTo SetValue
                    
            x = MultiPage1.Pages("Page_" & Region).Controls("TextBox_" & Region & "_Disc_" & DSSN & "_" & i).Value
            y = Evaluate("ROUND(SUMIFS(Metric_" & Region & "_SE_MDS,Attribute_PLLevel4,INDEX(L4_Values," & i & "))/" & SSMU & ",1)")
                    
            Resume Continue
                    
    SetValue:
        
            x = 0
            y = 0
        
    Continue:
                    
            With MultiPage1.Pages("Page_" & Region).Controls("TextBox_" & Region & "_Disc_" & DSSN & "_" & i)
                If x = y Then
                    .BackColor = RGB(223, 243, 249)
                Else
                    .BackColor = RGB(71, 142, 185)
                End If
            End With
                 
        Next i
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,261

    Re: Proper Error Handling Routine

    I don't know if this below is "proper", but try this concept:
    Option Explicit
    
    Dim MultiPage1 As Object '? Is there a declaration for this variable somewhere in your code ?
    Dim Region '? Is there a declaration for this variable somewhere in your code ?
    Dim DSSN '? Is there a declaration for this variable somewhere in your code ?
    Dim SSMU '? Is there a declaration for this variable somewhere in your code ?
    
    Sub HandleError_1()
        On Error Resume Next
            Dim arr: arr = Evaluate(ActiveWorkbook.Names("L4_Values").RefersTo) ' Is ActiveWorkbook = ThisWorkbook in this case ?
            If IsEmpty(arr) Then MsgBox "Something's wrong here - The End": Exit Sub
        On Error GoTo 0
        
        Dim is_arr As Boolean: is_arr = True
        Dim i&, indx&: If IsArray(arr) Then indx = UBound(arr, 1) Else is_arr = False: indx = 1
        Dim x: x = 0
        Dim y: y = 0
        
        On Error Resume Next
            For i = 1 To indx
                x = MultiPage1.Pages("Page_" & Region).Controls("TextBox_" & Region & "_Disc_" & DSSN & "_" & i).Value
                y = Evaluate("ROUND(SUMIFS(Metric_" & Region & "_SE_MDS,Attribute_PLLevel4,INDEX(L4_Values," & i & "))/" & SSMU & ",1)")
                If Err.Number <> 0 Then x = 0: y = 0 Else GoSub cmp_compare
            Next
        On Error GoTo 0
        
        If is_arr Then Erase arr Else arr = Empty
        
        Exit Sub
        
    cmp_compare:
        On Error GoTo 0: On Error GoTo whats_this
            With MultiPage1.Pages("Page_" & Region).Controls("TextBox_" & Region & "_Disc_" & DSSN & "_" & i)
                If x = y Then .BackColor = RGB(223, 243, 249) Else .BackColor = RGB(71, 142, 185)
            End With
        On Error GoTo 0: On Error Resume Next
        Return
        
    whats_this:
        Dim info$: info = "Error # " & Str(Err.Number) & " was generated by " & Err.Source & ":" & vbCrLf & vbCrLf & Err.Description
        MsgBox info, vbOKOnly, "Error", Err.HelpFile, Err.HelpContext
        MsgBox "This is THE END", vbOKOnly, " ... :(( ... :(( ..."
        End ' ... :) ... but be careful with "this" line ... :) ...
    End Sub
    Last edited by mjr veverka; 11-30-2018 at 06:26 AM. Reason: without colon after "Else" in "Else: is_arr = False: indx = 1"

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,953

    Re: Proper Error Handling Routine

    Sub HandleError()
        Dim x As Long
        Dim y As Long
        Dim i As Long
                
        For i = 1 To UBound(Evaluate(ActiveWorkbook.Names("L4_Values").RefersTo))
            On Error GoTo SetValue:
                    
            x = MultiPage1.Pages("Page_" & Region).Controls("TextBox_" & Region & "_Disc_" & DSSN & "_" & i).Value
            y = Evaluate("ROUND(SUMIFS(Metric_" & Region & "_SE_MDS,Attribute_PLLevel4,INDEX(L4_Values," & i & "))/" & SSMU & ",1)")
                    
            With MultiPage1.Pages("Page_" & Region).Controls("TextBox_" & Region & "_Disc_" & DSSN & "_" & i)
                If x = y Then
                    .BackColor = RGB(223, 243, 249)
                Else
                    .BackColor = RGB(71, 142, 185)
                End If
            End With
                 
        Next i
        Exit Sub
    SetValue:
        Err.Clear
        x = 0
        y = 0
        Resume Next
    End Sub
    Ben Van Johnson

  4. #4
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: Proper Error Handling Routine

    Thanks both!! will incorporate that into my code! Things seem to work so far.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Stop routine with error handling
    By Jan Wieland in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-08-2016, 06:50 AM
  2. barcodes handling as daily routine
    By jacohauritz in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 08-09-2016, 12:43 PM
  3. Proper application not handling apostrophes
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 05-29-2015, 09:57 AM
  4. Error handling inside error handling
    By grantastley in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2015, 03:43 AM
  5. [SOLVED] Error checking routine - check range values on various worksheets and report if error
    By TheRobsterUK in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-29-2014, 06:36 AM
  6. [SOLVED] Error Handling - On Error GoTo doesn't trap error successfully
    By David in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-16-2006, 02:10 PM
  7. Error handling with a handling routine
    By ben in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-15-2005, 11:06 AM

Tags for this Thread

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