Data validation - paste

jynxy

New Member
Joined
Feb 13, 2022
Messages
32
Office Version
  1. 2019
Platform
  1. Windows
Hi,

i have lots of dropdowns in different columns, i.e E6:E405 J6:J405 etc, the dropdown is using an if to check if holidays are allowed, depending if this is yes or no it will show one of two lists, one with holidays,lieu,unpaid and one without just showing things like shift swap etc.

because of this i need to prevent cells being overridden unless the value is in the list being used, this is because the holiday availability is now used and needs to be locked down, but i need to be able to enter the other options still, is there anyway to prevent the cells being overwritten ? ideally still being able to copy and paste dropdown values only.

Thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I have found this code below, this has mainly worked for what i need, i have few issues i can see so far,

a big issue i have with this, is if I'm using an if to display two different lists, these are both similar except the shorter one does not have holidays, lieu etc listed, with the below code if i copy and paste, it works as expected by not allowing unless in the list, now the problem is if the data is in the longer list and copied over a dropdown with the shorter list, it will still allow this.

second issues is Data validation / dropdown should not be deleted and remain as it was. i also need to allow blank if no option is to be selected. i can select blank which is fine, if i press delete it removes the dropdown, is this possible to make this blank only and leave the dropdown.

lastly i have around 31 columns of dropdowns so E2:E405 H2:H405 M2:M405 etc is there anyway to make this work for all of them without duplicating code ?

For example

Long ListShort List
HolidayShift Swap Out
LieuShift Swap In
Shift Swap OutBereavement
Shift Swap inAbsence
Bereavement
Absence


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ' Does the validation range still have validation?
    If Not Intersect(Range("E:E"), Target) Is Nothing Then
        Application.EnableEvents = False
        If HasValidation(Intersect(Range("E:E"), Target)) = False Then
            Application.Undo
            MsgBox "Your last operation was canceled." & vbCrLf & _
                "It would have deleted data validation rules.", vbCritical
        End If
    End If
    Application.EnableEvents = True
End Sub

Private Function HasValidation(r As Range) As Boolean
    ' Returns True if every cell in Range r uses Data Validation
    Dim c As Range
    Dim x As XlDVType
    On Error Resume Next
    For Each c In r.Cells
        x = r.Validation.Type
        If Err Then
            HasValidation = False
            Exit Function
        End If
    Next c
    HasValidation = True
End Function
 
Upvote 0

Forum statistics

Threads
1,215,227
Messages
6,123,745
Members
449,116
Latest member
alexlomt

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