Msg box when a cell value changes to >1

TODDLL

New Member
Joined
Apr 7, 2018
Messages
27
Office Version
  1. 365
  2. 2010
I would like a msg box to pop up if a cell's value is >1 In the table below as an example, If one of the people have a result entered more than once from Jan through Apr, I would like a pop up stating that there can only be 1 entry between Jan and April. The count column counts the cells if there is a value entered. I would like to apply this to a table that has 100 rows. The way I envision this working is if the user enters a second number in one of column cells, that a message will pop up when they hit enter. I have not worked with a msg box applied to a range of cells. I would surely appreciate help on this. Thank you.

Book1.xlsx
CDEFGH
5JANFEBMARAPRCOUNT
6JOHN1.241
7JIM1.71.32
8SAM2.51
9MIKE2.32.62
10KIM2.41
11SALLY2.11
Sheet1
Cell Formulas
RangeFormula
H6:H11H6=COUNT(D6:G6)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try something like this. Put it into the worksheet's code (Right click sheet name at the bottom -> View Code)
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns("D:G")) Is Nothing Then
        If Application.CountA(Range("D" & Target.Row & ":G" & Target.Row)) > 1 Then
            MsgBox "Only 1 entry allowed between Jan and Apr"
            Application.Undo ' Undo last entry, remove if not needed
        End If
    End If
End Sub
 
Upvote 0
Solution
Excellent! Even more than I had hoped for by undoing the entry. Now, how do I expand that so that it does the same for May through August, and September through December?
 
Upvote 0
Duplicate the If block, adjust the columns and message accordingly

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns("D:G")) Is Nothing Then
        If Application.CountA(Range("D" & Target.Row & ":G" & Target.Row)) > 1 Then
            MsgBox "Only 1 entry allowed between Jan and Apr"
            Application.Undo ' Undo last entry, remove if not needed
        End If
    End If
    If Not Intersect(Target, Columns("H:K")) Is Nothing Then '<-- adjust columns
        If Application.CountA(Range("H" & Target.Row & ":K" & Target.Row)) > 1 Then '<-- adjust columns
            MsgBox "Only 1 entry allowed between May and Aug"
            Application.Undo ' Undo last entry, remove if not needed
        End If
    End If
End Sub
 
Upvote 0
Just when I think I have it all figured out, I realized that I forgot to consider another factor. If the person has an "A" in the TYPE column, they can only have one entry in the 4 month period, however, if the person has a "B" in the TYPE column, they can have multiple entries during the 4 month period. Can that be worked into the vba code?
TYPEJANFEBMARAPRCOUNT
JOHNA1.241
JIMA1.71.32
SAMB2.51
MIKEB2.32.62
KIMA2.41
SALLYB2.11
 
Upvote 0
Just add in a new condition, assuming that's on column B.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns("D:G")) Is Nothing Then
        If Range("B" & Target.Row).Value = "A" Then
            If Application.CountA(Range("D" & Target.Row & ":G" & Target.Row)) > 1 Then
                MsgBox "Only 1 entry allowed between Jan and Apr"
                Application.Undo ' Undo last entry, remove if not needed
            End If
        End If
    End If
    If Not Intersect(Target, Columns("H:K")) Is Nothing Then '<-- adjust columns
        If Range("B" & Target.Row).Value = "A" Then
            If Application.CountA(Range("H" & Target.Row & ":K" & Target.Row)) > 1 Then '<-- adjust columns
                MsgBox "Only 1 entry allowed between May and Aug"
                Application.Undo ' Undo last entry, remove if not needed
            End If
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,214
Members
453,024
Latest member
Wingit77

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