vba macro to restrict mobile number entry in a cell range

Prasad K

Board Regular
Joined
Aug 4, 2021
Messages
189
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
I want a vba macro to restrict mobile number entry in a cell range like if I type 10 digits mobile number in a cell range of column B & it will accept the 10 digit mobile number entry

If I type less than or greater than 10 digits number in a cell range then it will show me a message with popup window

MsgBox (Please check the number you have Enter The Mobile should be contain 10 Digits only)

And one more if I enter same mobile number in a cell range then it will show me a message with popup MsgBox (You have Entered the Mobile Number is Already Exist in cell B4 , you want to continue with Duplicate Mobile Number click YES (or) want to remove Duplicate Mobile Number in EnireRow click NO)




Book1
AB
1Customer NameCustomer Mobile Number
2Ajay Kumar8521479630
3Arun Prasad2587413694
4Vikram Shah6222017896
5Govind Patel6321478952
6Ashok Kumar5467983120
7Sujatha Reddy5214639874
8Aparna Kumar7531598462
9Arjun Prasad Rao6899451124
10Vikram Shah6222017896
Sheet1
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 With Application
   If Not Intersect(Target, Range("B2:B1000")) Is Nothing And Target.Count = 1 And Len(Target) <> 10 Then
       .EnableEvents = False
        MsgBox "Please check the number you have Enter The Mobile should be contain 10 Digits only"
       .Undo
       .EnableEvents = True
       Exit Sub
   Else
       a = Application.Match(Target.Value, Range("B2:B1000"), 0)
       If IsNumeric(a) Then
           If MsgBox("You have Entered the Mobile Number is Already Exist in cell " & Cells(a, 2).Address(0, 0) & vbNewLine & "If you want to continue with Duplicate Mobile Number click (YES)" _
              & vbNewLine & "If want to remove Duplicate Mobile Number in EnireRow click (NO)", _
              vbQuestion + vbYesNo + vbDefaultButton2, "Duplicate Entry") = vbNo Then
              .EnableEvents = False
              .Undo
              .EnableEvents = True
           End If
       End If
   End If
 End With
End Sub
 
Upvote 0
Solution
Try this

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 With Application
   If Not Intersect(Target, Range("B2:B1000")) Is Nothing And Target.Count = 1 And Len(Target) <> 10 Then
       .EnableEvents = False
        MsgBox "Please check the number you have Enter The Mobile should be contain 10 Digits only"
       .Undo
       .EnableEvents = True
       Exit Sub
   Else
       a = Application.Match(Target.Value, Range("B2:B1000"), 0)
       If IsNumeric(a) Then
           If MsgBox("You have Entered the Mobile Number is Already Exist in cell " & Cells(a, 2).Address(0, 0) & vbNewLine & "If you want to continue with Duplicate Mobile Number click (YES)" _
              & vbNewLine & "If want to remove Duplicate Mobile Number in EnireRow click (NO)", _
              vbQuestion + vbYesNo + vbDefaultButton2, "Duplicate Entry") = vbNo Then
              .EnableEvents = False
              .Undo
              .EnableEvents = True
           End If
       End If
   End If
 End With
End Sub
it's working and here is a problem
if i type same mobile number in that cell range then it's not removing in entire row & one more thing if i type same mobile number in cell range then it will showing different cell value address in MsgBox
i am attaching screen shot please see
 

Attachments

  • 1.PNG
    1.PNG
    44 KB · Views: 9
Upvote 0
The entirerow thing will be tomorrow, the right cell:

change it to: Cell(a+1,2).Address
 
Upvote 0
change it to: Cell(a+1,2).Address
Not working
ok then ignore my question i have solved i have changed the code little bit

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 With Application
 On Error Resume Next
   If Not Intersect(Target, Range("B1:B1000")) Is Nothing And Target.Count = 1 And Len(Target) <> 10 Then
       .EnableEvents = False
        MsgBox "Please check the number you have Enter The Mobile should be contain 10 Digits only"
       .Undo
       .EnableEvents = True
       Exit Sub
   Else
       a = Application.Match(Target.Value, Range("B1:B1000"), 0)
       If IsNumeric(a) Then
           If MsgBox("You have Entered the Mobile Number is Already Exist in cell " & Cells(a, 2).Address(0, 0) & vbNewLine & "If you want to continue with Duplicate Mobile Number click (YES)" _
              & vbNewLine & "If want to remove Duplicate Mobile Number in EnireRow click (NO)", _
              vbQuestion + vbYesNo + vbDefaultButton2, "Duplicate Entry") = vbNo Then
              Target.Cells.EntireRow.Delete
              .EnableEvents = False
              .Undo
              .EnableEvents = True
           End If
       End If
   End If
 End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,440
Messages
6,124,882
Members
449,193
Latest member
PurplePlop

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