How do I skip errors using on error statements?

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
Office Version
  1. 365
Platform
  1. Windows
Gurus,
in my case here I want to use on error goto:


VBA Code:
For ss_line = 1 To 10
                             
                                Select Case ss_line
                            
                                Case 1 To 9
                               
                                cell1.Offset(0, ss_line + 3) = cell1.Offset(0, ss_line + 3) + _
                                WorksheetFunction.Index(tbl.DataBodyRange, WorksheetFunction.Match(cell.Value, tbl.ListColumns(3).DataBodyRange, 0), WorksheetFunction.Match("SS0" & ss_line, tbl.Range.Rows(1), 0))
                                                          on error goto notfound   
                                Case 10
                                cell1.Offset(0, ss_line + 3) = cell1.Offset(0, ss_line + 3) + _
                                WorksheetFunction.Index(tbl.DataBodyRange, WorksheetFunction.Match(cell.Value, tbl.ListColumns(3).DataBodyRange, 0), WorksheetFunction.Match("SS" & ss_line, tbl.Range.Rows(1), 0))
                                End Select
notfound:
                                Next ss_line

code does not seem to be working. Where do I place the on error statement or how do I find the right position to place it? Thank you!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I wouldn't use On Error to trap when the terms aren't found.
VBA Code:
Dim rowIndexNum as Variant, colIndexNum as Variant
'...
For ss_line = 1 to 10
    rowIndexNum = Application.Match(cell.Value, tbl.ListColumns(3).DataBodyRange, 0)
    colIIndexNum = Application.Match("SS" & Format(ss_line, "00"), tbl.Range.Rows(1), 0)
    If IsError(rowIndexNum) or IsError(colIndexNum) Then
        MsgBox "not there"
    Else
        cell1.Offset(0, ss_line + 3) = cell1.Offset(0, ss_line + 3) + tbl.DataBodyRange.Cells(rowIndexNum, colIndexNum)
    End If
Next ss_line
Note that Application.Match and WorksheetFunction.Match act differently when the search term is not found.
 
Upvote 0
I wouldn't use On Error to trap when the terms aren't found.
VBA Code:
Dim rowIndexNum as Variant, colIndexNum as Variant
'...
For ss_line = 1 to 10
    rowIndexNum = Application.Match(cell.Value, tbl.ListColumns(3).DataBodyRange, 0)
    colIIndexNum = Application.Match("SS" & Format(ss_line, "00"), tbl.Range.Rows(1), 0)
    If IsError(rowIndexNum) or IsError(colIndexNum) Then
        MsgBox "not there"
    Else
        cell1.Offset(0, ss_line + 3) = cell1.Offset(0, ss_line + 3) + tbl.DataBodyRange.Cells(rowIndexNum, colIndexNum)
    End If
Next ss_line
Note that Application.Match and WorksheetFunction.Match act differently when the search term is not found.
hi I tried application.match the thing is my calculation contains other variables so it's gonna pop type mismatch error regardless. Anyway to skip this alert?
 
Upvote 0
VBA Code:
On error Resume Next
For ss_line = 1 To 10
                             
                                Select Case ss_line
                            
                                Case 1 To 9
                               
                                cell1.Offset(0, ss_line + 3) = cell1.Offset(0, ss_line + 3) + _
                                WorksheetFunction.Index(tbl.DataBodyRange, WorksheetFunction.Match(cell.Value, tbl.ListColumns(3).DataBodyRange, 0), WorksheetFunction.Match("SS0" & ss_line, tbl.Range.Rows(1), 0))
                                Case 10
                                cell1.Offset(0, ss_line + 3) = cell1.Offset(0, ss_line + 3) + _
                                WorksheetFunction.Index(tbl.DataBodyRange, WorksheetFunction.Match(cell.Value, tbl.ListColumns(3).DataBodyRange, 0), WorksheetFunction.Match("SS" & ss_line, tbl.Range.Rows(1), 0))
                                End Select
Next ss_line
On Error Goto 0
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,930
Members
449,094
Latest member
teemeren

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