Data validation - applying & listing via VBA

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have a table and I'd like to apply data validation to the entire table column based on the header (ID, BirthDate, Sex, Source, etc).

I have two questions:

1) I have code from another project that I use for any situation, but I'm curious if there's a more efficient coding that I could use specifically for a table
Universal code:
Code:
Case .value Like "*SEX*"
  For Each cell In Intersect(hdrCell.EntireColumn, rngUsed).Offset(1, 0).Resize(rngUsed.Rows.Count - 1)
    ' ~~ apply data validation ~~

2) after applying the data validation, I'd like each new row to "inherit" the column data validation (critical):
A) without having to re-run the macro and
B) confine data validation to the table column only (not have entire column with data validation).

Thoughts anyone?

Thanks y'all.

As an aside, is there any way to create a list of data validation ranges/criteria? Kind of like what you can do with F3 | Paste List for named ranges/tables or what Jeff Weir did here (Easy way to back up CF formats)??
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I when data validation is applied to a table subsequent rows added to that table will have inherit that validation, but if multiple rows are added (pasted) to the table at once, the data validation exists, but does not change any pasted data, it is applied only if changes are made to the pasted data.

Code:
Very crude code.  Review Validation Object in VBA help to expand

Sub DocumentCellValidation()

    Dim rngCell As Range
    Dim x
    
    On Error Resume Next
    For Each rngCell In ActiveSheet.UsedRange.Cells
        x = rngCell.Validation
        If Err.Number <> 0 Then Debug.Print rngCell.Address, rngCell.Validation.Type
        Err.Clear
    Next
    
End Sub
 
Upvote 0
Thanks so much! That's too easy (when someone else provides the answer ;)

However, with applying validation to new rows -- since each column has different validation (mostly from lists), would I use OFFSET to "copy" the validation from the cell above and apply it, column by column, to the new row??
 
Last edited:
Upvote 0
When you create a table and establish (possibly different) validation in each cell in the first row, each column's unique validation is carried down that column as the table is expanded. Tested with Excel 2010.
 
Upvote 0
So by adding a row to the bottom of the table and then pasting the data in the new, empty row, it will automatically inherit the validation for each column? Cool!

Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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