Avoiding using Cases for Data Validation.

sbo1985

New Member
Joined
Aug 7, 2015
Messages
25
Hello all,
I am trying to prevent copy/paste over data validation. I have the following code and it works perfect. I can not run a loop because I have different columns for data validations. I can not select a non-contigious range neither because the formula doesn't allow me to do so when I try make changes in drop down list. Is there a way to shorten the following code?

Code:
Select Case Target.Column
Case Is = 3
     Set Rng1 = Sheets("NCR Log").Range("C4:C1048576")
     If HasValidation(Rng1) Then
     'do nothing
     Else
     Application.EnableEvents = False
     MsgBox "Your last operation was canceled." & vbNewLine & _
        "It would have violated data validation rules.", vbCritical
     Application.Undo
     Application.EnableEvents = True
     End If
     Case Is = 4
     Set Rng2 = Sheets("NCR Log").Range("D4:D1048576")
     If HasValidation(Rng2) Then
     'do nothing
     Else
     Application.EnableEvents = False
      MsgBox "Your last operation was canceled." & vbNewLine & _
        "It would have violated data validation rules.", vbCritical
     Application.Undo
     Application.EnableEvents = True
     End If
     Case Is = 5
     Set Rng3 = Sheets("NCR Log").Range("E4:E1048576")
     If HasValidation(Rng3) Then
     'do nothing
     Else
     Application.EnableEvents = False
      MsgBox "Your last operation was canceled." & vbNewLine & _
        "It would have violated data validation rules.", vbCritical
     Application.Undo
     Application.EnableEvents = True
     End If
      Case Is = 6
     Set Rng4 = Sheets("NCR Log").Range("F4:F1048576")
     If HasValidation(Rng4) Then
     'do nothing
     Else
     Application.EnableEvents = False
      MsgBox "Your last operation was canceled." & vbNewLine & _
        "It would have violated data validation rules.", vbCritical
     Application.Undo
     Application.EnableEvents = True
     End If
     Case Is = 10
     Set Rng5 = Sheets("NCR Log").Range("J4:J1048576")
     If HasValidation(Rng5) Then
     'do nothing
     Else
     Application.EnableEvents = False
      MsgBox "Your last operation was canceled." & vbNewLine & _
        "It would have violated data validation rules.", vbCritical
     Application.Undo
     Application.EnableEvents = True
     End If

Thanks in advance,
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Thank you for your response,
However, my ranges change with every case, how should I tackle with this?
 
Upvote 0
It looks like your ranges are always in the column of Target.

Instead of Sheets("NCR Log").Range("D4:D1048576"), use Target.EntireColumn.Range("A4:A1048576")


1) In that usage, the .Range is relative to Target.EntireColumn, so it's not always column A, but the same column as Target.

2) An alternate syntax would be
Code:
With Target.EntireColumn
    Set myRange = Range(.Cells(4, 1), .Cells(Rows.Count, 1))
End With

I'm also noticing that inside each of the OP cases, you are setting a different range variable, Rng3, Rng4, etc.. You could use one variable for all of the cases.
 
Upvote 0

Forum statistics

Threads
1,213,486
Messages
6,113,932
Members
448,533
Latest member
thietbibeboiwasaco

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