show massage in worksheet change after three times select cell and close the file

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
I need help from the experts to how I add message box after three times of tries to select cells in column a and close the file
this is my code so far
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Columns(1)) Is Nothing Then Exit Sub
If Not Intersect(Target, Columns(1)) Is Nothing Then
MsgBox "you  can't  add  any  values": Target.Value = Range("b2").Select: Target.Value = "": Exit Sub
End If

End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
How about
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Static Counter As Long
   If Not Intersect(Target, Columns(1)) Is Nothing Then
      If Counter < 3 Then
         MsgBox "you  can't  add  any  values"
         Counter = Counter + 1
      Else
         MsgBox "Goodbye"
         ThisWorkbook.Close
      End If
   End If
End Sub
 
Upvote 0
Solution
VBA Code:
Private N As Long

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Columns(1)) Is Nothing Then Exit Sub

    If Not Intersect(Target, Columns(1)) Is Nothing Then
    N = N + 1
    If N > 3 Then N = 1
    MsgBox "you  can't  add  any  values": Target.Value = Range("b2").Select: Target.Value = ""
    If N = 3 Then
   
    ThisWorkbook.Close , True

    End If
End If

End Sub
 
Last edited:
Upvote 0
@Fluff & @Dossfm your codes are both work but if is possible I don't populate the message "save ,no save , cancel" , I would directly close without any populate
 
Upvote 0
Do you want to save the file?
 
Upvote 0
In that case use
VBA Code:
         ThisWorkbook.Close False
But if a user has made a number of changes to the workbook, they will loose it all. :eek:
 
Upvote 0
@Fluff to understand why I don't save , this is kind of protection if anybody tries write into the file this code prevents I know I can do by protection the file , but I would test different way and you and Dossfm did that
many thanks guys for your assistance ;)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
more protection type "Yourusername"
VBA Code:
If N = 3 And Environ("username") <> "Yourusername" Then
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

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