When A Formula Errs (Error Handling?)

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Consider this code:
Rich (BB code):
Private Sub UserForm_Initialize()
    Dim sed As Long
    Dim srng As Range
    Dim cloc As Range
    
    Me.Frame1.Caption = "Location:  " & ws9.Name & "  " & cell.Address
    Me.f1name = cval2
    hh = Application.WorksheetFunction.Index(ws_staff.Range("D4:D33"), WorksheetFunction.Match(cval2, ws_staff.Columns(4), 0))
    On Error Resume Next
    Me.f1shift = hh
    'Stop
    qrw = cell.Row
    trid = ws_working.Cells(qrw, 1)
    tpn = ws_working.Cells(qrw, 3)
    tpgm = ws_working.Cells(qrw, 5)
    tfac = ws_working.Cells(qrw, 4)
    ttms = Format(ws_working.Cells(qrw, 6), "h:mmA/P") & " - " & Format(ws_working.Cells(qrw, 7), "h:mmA/P")
    assgnt = ws_working.Cells(10, cell.Column)
    assgnt = ws_working.Cells(10, cell.Column)
    If IsNumeric(assgnt) = True Then assgnt = "TrnService" & assgnt
    Me.f1assgnt = assgnt
    Me.f1detail1 = trid & "  " & tpn & "  " & tpgm
    Me.f1detail2 = ttms & "  " & tfac
    Me.f1name.Locked = True
    Me.f1assgnt.Locked = True
    Me.f1shift.Locked = True
    Me.f1detail1.Locked = True
    Me.f1detail2.Locked = True
    
    'create combobox list
    sed = ws_staff.Cells(ws_staff.Rows.Count, "V").End(xlUp).Row
    Set srng = ws_staff.Range("V4:V" & sed)
    For Each cloc In srng
        With Me.ComboBox1
            .AddItem cloc.Value
        End With
    Next cloc
    
End Sub

The line in blue will err if there is no match. The error can be expected, so if that line errs, I want the value of hh to be "NA"

Can anyone suggest an improvement to my code to accomplish what I need to do for the value of hh?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
I would do this.

Rich (BB code):
Private Sub UserForm_Initialize()
    Dim sed As Long
    Dim srng As Range
    Dim cloc As Range
    
    Me.Frame1.Caption = "Location:  " & ws9.Name & "  " & cell.Address
    Me.f1name = cval2
    hh = "NA"
    On Error Resume Next
    hh = Application.WorksheetFunction.Index(ws_staff.Range("D4:D33"), WorksheetFunction.Match(cval2, ws_staff.Columns(4), 0))
    On Error GoTo 0
    On Error Resume Next ' why do you have this here? Potentially dangerous.
    Me.f1shift = hh
    'Stop
    qrw = cell.Row
    trid = ws_working.Cells(qrw, 1)
    tpn = ws_working.Cells(qrw, 3)
    tpgm = ws_working.Cells(qrw, 5)
    tfac = ws_working.Cells(qrw, 4)
    ttms = Format(ws_working.Cells(qrw, 6), "h:mmA/P") & " - " & Format(ws_working.Cells(qrw, 7), "h:mmA/P")
    assgnt = ws_working.Cells(10, cell.Column)
    assgnt = ws_working.Cells(10, cell.Column)
    If IsNumeric(assgnt) = True Then assgnt = "TrnService" & assgnt
    Me.f1assgnt = assgnt
    Me.f1detail1 = trid & "  " & tpn & "  " & tpgm
    Me.f1detail2 = ttms & "  " & tfac
    Me.f1name.Locked = True
    Me.f1assgnt.Locked = True
    Me.f1shift.Locked = True
    Me.f1detail1.Locked = True
    Me.f1detail2.Locked = True
    
    'create combobox list
    sed = ws_staff.Cells(ws_staff.Rows.Count, "V").End(xlUp).Row
    Set srng = ws_staff.Range("V4:V" & sed)
    For Each cloc In srng
        With Me.ComboBox1
            .AddItem cloc.Value
        End With
    Next cloc
    
End Sub
 
Upvote 0
Solution
Another option.

Replace this.
VBA Code:
    hh = Application.WorksheetFunction.Index(ws_staff.Range("D4:D33"), WorksheetFunction.Match(cval2, ws_staff.Columns(4), 0))

With this:
VBA Code:
    If Not IsError(Application.Index(ws_staff.Range("D4:D33"), Application.Match(cval2, ws_staff.Columns(4), 0))) Then
        hh = Application.Index(ws_staff.Range("D4:D33"), Application.Match(cval2, ws_staff.Columns(4), 0))
    Else
        hh = "NA"
    End If

Note that the replacement code does not contain the term "WorksheetFunction"
 
Upvote 0
That worked! Thanks again Jeff, you help is always appreciated.
 
Upvote 0

Forum statistics

Threads
1,216,816
Messages
6,132,862
Members
449,761
Latest member
AUSSW

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