Stop Error when no Data Found

carpjm1

New Member
Joined
Sep 12, 2014
Messages
5
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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
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"
 
Upvote 0
I dont know how to code this in VBA, but you need to test for something like if(cell="","",rest-of-the-code
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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