• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Protect the cell

Abhijeet

Active Member
I want no other data copy paste in cell containing the Data Validation.Only data validation name can input in the cell where is the data validation.
 
Good day Abhijeet
This may be of help to you.....I am not the author......

This is a bit involved so bear with me. The bottom line here is that
you need to disable the Paste and Paste Special menu commands whenever the
user selects one of the Data Validation cells. This is easy enough to do
but it comes with some baggage that you have to take care of. This
"baggage" is that disabling a menu command is a global setting in Excel. By
this I mean that the new setting (disable so-and-so) applies to Excel, not
just to the file that you are working with.
In short, if the user selects one of your Data Validation cells, thereby
disabling the Paste commands, and then chooses to activate or open another
Excel file, that new file will not have the Paste commands available. So
you must have code (macros) to do the following:
Disable the two Paste commands if a Data Validation cell is selected.
Enable the two Paste commands if any other cell is selected.
Enable the two Paste commands if any other file is activated or opened.
Enable the two Paste commands if your file is closed
Select a non Data Validation cell before the file is closed or another file
is activated.

In the following macros, I chose A1 as a non Data Validation cell. I also
chose "TheRng" as the range name of the range of all of your Data Validation
cells, so you have to select that range and name it TheRng.

I also chose "TheSheetName" as the name of your sheet.

I also chose "TheFileName.xls" as the name of your file.

You have to change these names in these macros to make them work with your
file and sheet.

Put the following macro in the sheet module of your sheet:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("TheRng")) Is Nothing Then
Call Enable_Disable_Commands(22, False) 'CANNOT Paste
Call Enable_Disable_Commands(755, False) 'CANNOT Paste Special
Else
Call Enable_Disable_Commands(22, True) 'CAN Paste
Call Enable_Disable_Commands(755, True) 'CAN Paste Special
End If
End Sub

Put the following two macros in the Workbook module of your file:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Enable_Disable_Commands(22, True) 'CAN Paste
Call Enable_Disable_Commands(755, True) 'CAN Paste Special
Sheets("TheSheetName").Activate
Range("A1").Select
ThisWorkbook.Save
ThisWorkbook.Saved = True
End Sub
 
Private Sub Workbook_Deactivate()
Call SelectA1
End Sub

Put the following two macros in a regular module of your file:
Code:
Sub Enable_Disable_Commands(id As Integer, Enab As Boolean)
Dim myControls As CommandBarControls
Dim ctl As CommandBarControl
Set myControls = CommandBars.FindControls _
(Type:=msoControlButton, id:=id)
For Each ctl In myControls
ctl.Enabled = Enab
Next ctl
End Sub
 
Sub SelectA1()
Dim NewFile As String
Application.ScreenUpdating = False
NewFile = ActiveWorkbook.Name
Windows("TheFileName.xls").Activate
Sheets("TheSheetName").Activate
Range("A1").Select
Application.EnableEvents = False
Windows(NewFile).Activate
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
 
Back
Top