VBA Matching Text in Cell to a Error Msg

Tech1_uk

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

I'm trying to do something very simple but I just can't get this to work.

I have a file that is import into a sheet and then the following is run to look for a specific text and then copy it to a new sheet.
VBA Code:
' Selectdata
'
Dim lr As Long
Dim Fnd As Range

    lr = Worksheets("C_Data").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Set Fnd = Worksheets("C_Data").Rows(1).Find("VOLTAGE 01", lookat:=xlPart)
        If Fnd Is Nothing Then MsgBox "VOLTAGE Not found in C Data", vbExclamation: Exit Sub
    
    Fnd.Resize(lr, 96).Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
ActiveSheet.Name = "Data"

This works well and does what I want it too but I want to add another check.

The data that is imported will look like this,

Monitor ItemBATT LEVEL (%)VOLTAGE 01VOLTAGE 02VOLTAGE 03VOLTAGE 04VOLTAGE 05VOLTAGE 06
048391439143914391039093914
0.0548391439143914391039093914
0.148391439143914NO_VALUE39093914
0.1548391439143914391039093914
0.248391439143914391039093914
0.2548391439143909391039093914
0.348391439143909391039093914

What I need to do is the "NO_VALUE" ( shown above in VOLTAGE 4 column) is displayed in any cell, then to display a error msg then exit.

I guess it should be run before the code above and before the new sheet is created.

Currently I've tried all sorts and can not get this to work.

Could someone be so kind to point me in the right direction as in theory this should be so simple!!!

Many thanks

Steve
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Maybe:
VBA Code:
Dim lr As Long
Dim Fnd As Range
    If Application.CountIf(ActiveSheet.UsedRange, "NO_VALUE") Then
        MsgBox "At least one cell contains NO_VALUE - exiting sub"
        Exit Sub
    End If
    lr = Worksheets("C_Data").Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Set Fnd = Worksheets("C_Data").Rows(1).Find("VOLTAGE 01", lookat:=xlPart)
        If Fnd Is Nothing Then MsgBox "VOLTAGE Not found in C Data", vbExclamation: Exit Sub
    
    Fnd.Resize(lr, 96).Copy
Sheets.Add After:=ActiveSheet
ActiveSheet.Paste
ActiveSheet.Name = "Data"
 
Upvote 0
How about
VBA Code:
   Dim lr As Long
   Dim Fnd As Range

   With Worksheets("C_Data")
      Set Fnd = .Cells.Find("NO_value", , , xlWhole, , , False, , False)
      If Not Fnd Is Nothing Then
         MsgBox "Error"
         Exit Sub
      End If
      lr = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
      Set Fnd = .Rows(1).Find("VOLTAGE 01", lookat:=xlPart)
   End With
   If Fnd Is Nothing Then MsgBox "VOLTAGE Not found in C Data", vbExclamation: Exit Sub
   
   Fnd.Resize(lr, 96).Copy
   Sheets.Add After:=ActiveSheet
   ActiveSheet.Paste
   ActiveSheet.Name = "Data"
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,284
Messages
6,124,064
Members
449,139
Latest member
sramesh1024

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