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

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
989
Office Version
  1. 2010
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
 

Some videos you may like

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,741
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
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:

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
989
Office Version
  1. 2010
@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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,741
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Do you want to save the file?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,741
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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:
 

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
989
Office Version
  1. 2010
@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 ;)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,741
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
more protection type "Yourusername"
VBA Code:
If N = 3 And Environ("username") <> "Yourusername" Then
 

Watch MrExcel Video

Forum statistics

Threads
1,128,057
Messages
5,628,359
Members
416,314
Latest member
Dan99321

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
Top