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

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Momentman

Well-known Member
Joined
Jan 11, 2012
Messages
4,142
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
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

FDibbins

Well-known Member
Joined
Feb 16, 2013
Messages
6,723
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,191,216
Messages
5,985,318
Members
439,956
Latest member
venky2002

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
Top