# Stop Error when no Data Found

#### carpjm1

##### New Member
Hi All. Can you please enlighten me on how to stop this from erroring out because the sheet did not contain the search data. Thank you!

Code:
``````    Dim wsb As Worksheet
Dim foundColb As Integer
Dim lastRowb As Long
Dim jb As Long
Dim accumb As Long

'sum cbcp totals and apply to totals tab
Sheets("BCPData").Activate
Set wsb = ActiveSheet
[COLOR=#ff0000]foundColb = wsb.Cells.Find("CBCP").Column
[/COLOR]   lastRowb = wsb.Cells(Rows.Count, foundColb).End(xlUp).Row
For jb = 1 To lastRowb
If InStr(1, LCase(wsb.Cells(jb, foundColb)), "cbcp") > 0 Then
accumb = accumb + CLng(wsb.Cells(jb, foundColb + 1))
End If
Next jb
Sheets("Totals").Activate
Range("C59").Value = accumb * -0.01``````

Last edited:

### Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try like this
Rich (BB code):
``````'sum cbcp totals and apply to totals tab
Sheets("BCPData").Activate
Set wsb = ActiveSheet
On Error GoTo errhandler
foundColb = wsb.Cells.Find("CBCP").Column
lastRowb = wsb.Cells(Rows.Count, foundColb).End(xlUp).Row
For jb = 1 To lastRowb
If InStr(1, LCase(wsb.Cells(jb, foundColb)), "cbcp") > 0 Then
accumb = accumb + CLng(wsb.Cells(jb, foundColb + 1))
End If
Next jb
Sheets("Totals").Activate
Range("C59").Value = accumb * -0.01
errhandler:
MsgBox "No data found"``````

I dont know how to code this in VBA, but you need to test for something like if(cell="","",rest-of-the-code

Replies
6
Views
440
Replies
4
Views
548
Replies
2
Views
404
Replies
0
Views
383
Replies
4
Views
935

1,219,792
Messages
6,150,290
Members
450,949
Latest member
faizanmalik10

### 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?

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