VBA Code to Search for specific Text and Display a Messagebox with outcome

fishandtril

New Member
Joined
Nov 15, 2022
Messages
13
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello
I am trying to write a macro that will look at data in a specifc worksheet and range and present a message box if the outcome is TRUE
I have data that is deemed "valid" or "invalid" depending on the data.
I want to remove the msb box if the "invalid" is contained in the range and nothing else needs to be done.
Here is my code:

VBA Code:
Sub Test()
    Dim FoundRange As Range
    Dim SearchTerm As String
    
    'Look for something
    SearchTerm = "Invalid"
    Set FoundRange = Worksheets("WWC").Range("H:H").Find(What:=SearchTerm)
    
    'If found
    If Not FoundRange Is Nothing Then
        
       MsgBox "'" & SearchTerm & "' not found"
    Else
        MsgBox "'" & SearchTerm & "' found"
    End If
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi,
You can search through a range by use of a for loop, something like
VBA Code:
for each cellRef in foundRange.cells
....
next
Then in the loop put in your data checks with something like
VBA Code:
if cellRef.value = SearchTerm Then
  found = true
end if
You may want to then exit the for loop as soon as it's found once to save searching the entire range.
You might want to also limit the range to the last used range of cells in the sheet rather than the entire 1M rows if the whole column
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,085
Members
449,206
Latest member
ralemanygarcia

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