Display VBA msgbox when cell contains text

histoman52

New Member
Joined
Jan 19, 2022
Messages
1
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello, apologies for any errors this is my first post and I am relatively new to VBA.

I work in a lab and we use a spreadsheet for recording some tests that need to be sent away. I need a msgbox to appear when a specific test is recorded "HER2" to remind the user to check if another test is also required. I have managed to do this, however only if the cell contains exactly "HER2" or "her2" and the cell may not always contain exactly this as there are a few variations of how it is entered (Gastric HER2, HER-2, Gas HER2 etc). Is there a way to have this recognise the HER bit even if it contains other text as this will always be included and trigger the msgbox.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xCell As Range, Rg As Range
    On Error Resume Next
    Set Rg = Application.Intersect(Target, Range("C1:C9999"))
    If Not Rg Is Nothing Then
        For Each xCell In Rg
            If UCase(xCell.Value) = "HER2" Then
                MsgBox "Gastric HER2 requested, check with requestor if MMR required"
                Exit Sub
            End If
        Next
   End If
End Sub

Thanks :)
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
try
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xCell As Range, Rg As Range
    On Error Resume Next
    Set Rg = Application.Intersect(Target, Range("C1:C9999"))
    If Not Rg Is Nothing Then
        For Each xCell In Rg
            'If UCase(xCell.Value) = "HER2" Then
            If InStr(1, UCase(xCell.Value), "HER") > 0 Then
                MsgBox "Gastric HER2 requested, check with requestor if MMR required"
                Exit Sub
            End If
        Next
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,117
Messages
6,128,937
Members
449,480
Latest member
yesitisasport

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