Message Box if cells contain criteria

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

Is it possible to do the following:

If A3:A5 contains X then message box shows hit
If K2:N2 contains X then message box shows hit
If K10:K14 contains X then message box shows hit
Else show Miss

Thanks
 

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.
TRY:
Code:
Sub Messages()
    Dim a, cel As Range, msg As String
    For Each a In Array("A3:A5", "K2:N2", "K10:K14")
        For Each cel In Range(a)
            If WorksheetFunction.CountIf(cel, "[COLOR=#ff0000]x[/COLOR]") > 0 Then
                msg = msg & vbCr & a & " HIT"
                Exit For
            End If
            With Range(a)
                If cel.Address = .Cells(.Count).Address Then msg = msg & vbCr & a & " MISS"
            End With
        Next cel
    Next a
    MsgBox msg
End Sub

Returns a single message box detailing status for all ranges
Asuumes that "x" is a consistent value
 
Last edited:
Upvote 0
or
If one HIT is enough, and you do not care where...
Code:
Sub Messages2()
    Dim a, cel As Range, msg As String
    For Each a In Array("A3:A5", "K2:N2", "K10:K14")
        For Each cel In Range(a)
            If WorksheetFunction.CountIf(cel, "x") > 0 Then
                msg = "HIT"
                GoTo msg
            End If
        Next cel
    Next a
    msg = "MISS"
msg:
    MsgBox msg
End Sub

Asuumes that "x" is a consistent value

You requested that a cell "contains X", hence
Code:
 If WorksheetFunction.CountIf(cel, "x") > 0 Then
For "equals x" use
Code:
 If cel.value = "x" Then
(applies to both solutions)
 
Last edited:
Upvote 0
Thanks for this, its nearly perfect but i literally just want the message box to show 'Hit' or 'Miss' and nothing else. So if A3 has an X show 'Hit, if A4 has an X show 'Hit, ect if not show Miss. At the moment the message box shows:

A3:A5 HIT
K2:N2 MISS
K10:K14 MISS
 
Upvote 0
Sorry i should also metion that all of my data sits within range a1 to am28. So essentially if an X is within "A3:A5", "K2:N2", "K10:K14" then show HIT, else if an X is within any other cell within A1 to AM28 then show miss
 
Upvote 0
no, the x could be anywhere within the range but if its specially in the range "A3:A5" or "K2:N2" or "K10:K14" then the msg should show Hit but if someone enters an x anywhere else in the range, it should show miss. I'm basically creating the board game battleships within excel as a competition for staff. No worries if this can not be done its a nice to have and not essential.
 
Upvote 0
Our posts crossed
I think code in post3 will return what you want - let me know if it doesn't
 
Upvote 0
thanks so i'm using the below code and the message shows hit if there is an x in range "A3:A5" or "K2:N2" or "K10:K14" but also shows HIT if there is an X in any other cell rather than miss

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim a, cel As Range, msg As String
    For Each a In Array("A3:A5", "K2:N2", "K10:K14")
        For Each cel In Range(a)
            If cel.Value = "x" Then
                msg = "HIT"
                GoTo msg
            End If
        Next cel
    Next a
    msg = "MISS"
msg:
    MsgBox msg
End Sub
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim a, msg As String:       msg = "MISS"
    
    If Not Intersect(Target, Range("A1:AM28")) Is Nothing Then
        If UCase(Target.Value) <> "X" Then Exit Sub
        For Each a In Array("A3:A5", "K2:N2", "K10:K14")
            If Not Intersect(Target, Range(a)) Is Nothing Then msg = "HIT"
       Next a
       MsgBox msg
    End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,992
Messages
6,175,831
Members
452,673
Latest member
LaMiaAvy

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