VBA code for Data Validation Error

sikander1986

Board Regular
Joined
Sep 6, 2008
Messages
66
Hi,
I am using office 2003 & have data validations in different columns. The problem is that whenever anyone copy & paste anything in these columns the validations goes. And this is making the file size large & difficult to work.
I want to get data validations by using VBA codes & disable the normal pasting in these columns. And if anyone try to paste anything by way of normal copy & paste or paste special then they should get error message as "You are not authorize to do this function".

For Example:
In column B from B2, I have validation Yes & No, and in column D from D2 I have data validation Morning, Afternoon & Evening. The Data source for Column B is from Column G i.e. from G2 to G5 & for Column D it is from Column H i.e. from H2 to H5. Below the sample of my recording macro.
Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+t
'
    Range("B2").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=$G$2:$G$5"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
    Range("D2").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=$H$2:$H$5"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub
I want to get this code to work in such a way that if Column A is filled then the column B should not be blank & if its blank give the error Message as " B2 can't be blank & don't allow to save the file and if Column C is filled then the column D should not be blank & it its blank give the error message as "D2 can't be blank & don't allow to save the file.
I wan looking this to get this done in the following way of macro format.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub
Thank you in advance...:cool:
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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