Error Handling When Find Command Doesn't Return a Result - VBA

Tech1_uk

New Member
Joined
Sep 22, 2018
Messages
17
Hi All,

Someone on this forum has kindly help me with a find command for some data that is variable.

The problem I have now is if the code doesn't match a string it then errors. What I need is when the string is not found to display a Msgbox with an error then back to the user form.
I've tried many ways but I can't it too work.

Below is the code I'm using which is searching for the string "Cell Voltage 01" in row 1 of an activesheet. so if "Cell Voltage 01" is not found display msgbox. If the data is found it copy and paste to a new sheet and it is then turned into a chart.
Sounds simple and probably is, but I just can't work it out.

Cheers

Code:
' Selectdata
'
Dim lr As Long
    lr = Worksheets("Consult_Data").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Worksheets("Consult_Data").Rows(1).Find("CELL VOLTAGE 01", lookat:=xlPart).Resize(lr, 96).Select
 Selection.Copy
 Sheets.Add After:=ActiveSheet
 ActiveSheet.Paste
 ActiveSheet.Name = "Data"
 

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"
Try
Code:
Dim lr As Long
Dim Fnd As Range
    lr = Worksheets("Consult_Data").Cells.find("*", , xlValues, , xlByRows, xlPrevious).Row
    Set Fnd = Worksheets("Consult_Data").Rows(1).find("CELL VOLTAGE 01", lookat:=xlPart)
    If Fnd Is Nothing Then MsgBox "Not found": Exit Sub
    Fnd.Resize(lr, 96).copy
 Sheets.Add After:=ActiveSheet
 ActiveSheet.Paste
 ActiveSheet.Name = "Data"
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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