Best Practices for Error Handling

antman2988

Board Regular
Joined
Jun 28, 2018
Messages
78
Hello. I have the code below that first checks to see if a certain field (header) exists before changing the formatting. Should I perform the same error checking for the fields at the bottom? Is there a better way to handle errors?

VBA Code:
Private Sub FormatColumns(ByVal tbl As ListObject, ByVal budDt As String, ByVal acctDt As String, ByVal jrnlDt As String, ByVal issDt As String, ByVal transDt As String)
    Dim foundBudDt As Range
    Dim foundAcctDt As Range
    Dim foundJrnlDt As Range
    Dim foundIssDt As Range
    Dim foundTransDt As Range
    
    'Attempt to find string values in table header row
    On Error Resume Next
    Set foundBudDt = tbl.HeaderRowRange.Find(budDt, _
        LookAt:=xlWhole)
    Set foundAcctDt = tbl.HeaderRowRange.Find(acctDt, _
        LookAt:=xlWhole)
    Set foundJrnlDt = tbl.HeaderRowRange.Find(jrnlDt, _
        LookAt:=xlWhole)
    Set foundIssDt = tbl.HeaderRowRange.Find(issDt, _
        LookAt:=xlWhole)
    Set foundTransDt = tbl.HeaderRowRange.Find(transDt, _
        LookAt:=xlWhole)
    On Error GoTo 0
    
    'Modify formats of header data body ranges based on specified parameters
    'Set formats to dates
    With foundBudDt
        tbl.ListColumns(budDt).DataBodyRange.NumberFormat = "m/d/yyyy"
    End With
    
    With foundAcctDt
        tbl.ListColumns(acctDt).DataBodyRange.NumberFormat = "m/d/yyyy"
    End With
    
    With foundJrnlDt
        tbl.ListColumns(jrnlDt).DataBodyRange.NumberFormat = "m/d/yyyy"
    End With
    
    With foundIssDt
        tbl.ListColumns(issDt).DataBodyRange.NumberFormat = "m/d/yyyy"
    End With
    
    With foundTransDt
        tbl.ListColumns(transDt).DataBodyRange.NumberFormat = "m/d/yyyy"
    End With
    
    'Format nonessential columns
    tbl.ListColumns("Amount").DataBodyRange.NumberFormat = "$#,##0.00_);($#,##0.00)"
    tbl.ListColumns("Svc Loc").DataBodyRange.NumberFormat = "00000"
    tbl.ListColumns("Fund").DataBodyRange.NumberFormat = "0000"
    tbl.ListColumns("Activity").DataBodyRange.NumberFormat = "000"
    tbl.ListColumns("Class Code").DataBodyRange.NumberFormat = "0000"
End Sub

Thank you.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I would recommend getting rid of these two lines
VBA Code:
    On Error Resume Next

    On Error GoTo 0
and then change the With Statements to If statements like
VBA Code:
    If Not foundBudDt Is Nothing Then
        tbl.ListColumns(budDt).DataBodyRange.NumberFormat = "m/d/yyyy"
    End If
 
Upvote 0
Solution
I would recommend getting rid of these two lines
VBA Code:
    On Error Resume Next

    On Error GoTo 0
and then change the With Statements to If statements like
VBA Code:
    If Not foundBudDt Is Nothing Then
        tbl.ListColumns(budDt).DataBodyRange.NumberFormat = "m/d/yyyy"
    End If
Thanks for the help Fluff. Should I apply the same error handling to the bottom lines? Pretty sure they'll just throw errors if they don't exist, otherwise. Right?
 
Upvote 0
It's not really error handling. Just replace the with statements as I showed.
 
Upvote 0
Not IMO, it's preventing errors if the budDt column doesn't exist, rather than handling an error once it's occurred.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top