populate message box if target bigger than 3 times for the same name

tubrak

Board Regular
Joined
May 30, 2021
Messages
216
Office Version
  1. 2019
Platform
  1. Windows
Hi experts ,
I want when I repeat the same name in column C bigger than three times , then should populate message box " you can't repeat more than three times" and clear target and if not , then should pops message " you still can add within allowed limiting " and continue add repeating name .
I have this code but doesn't work as what I want it .
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Target.Column = 3 And Target.Row > 1 Then
Application.DisplayAlerts = False
With ThisWorkbook.Sheets("Sheet1")
    lr = .Cells(.Rows.Count, "C").End(xlUp).Row
   ' If Target.Value = "" Then Exit Sub
    If .Evaluate("=Max(countif(C2:C" & lr & ",C2:C" & lr & "))") > 3Then
    
       MsgBox "you  can't  repeat more than three times ! " & Target.Value
       Target.Value = ""
       Exit Sub
      
       Else
        MsgBox "you  still can  add within allowed limiting !"
    End If
     Application.DisplayAlerts = True
     
End With
End If
End Sub
I look forward from experts to fix it.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lr As Long
    Dim rng As Range

    If Target.CountLarge > 1 Then Exit Sub
    
    If Target.Column = 3 And Target.Row > 1 Then
        lr = Cells(Rows.Count, "C").End(xlUp).Row
        Set rng = Range("C2:C" & lr)
        If Application.WorksheetFunction.CountIf(rng, Target) > 3 Then
            Application.EnableEvents = False
            Target.ClearContents
            Application.EnableEvents = True
            MsgBox "You can't repeat more than three times!" & Target.Value
        Else
            MsgBox "You still can add within allowed limiting!"
        End If
    End If

End Sub
 
Upvote 1
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,824
Messages
6,127,108
Members
449,359
Latest member
michael2

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