Skip to content

Instantly share code, notes, and snippets.

@supergrass71
Last active March 12, 2019 13:25
Show Gist options
  • Save supergrass71/8d4b41af06e5c65145c26402be9cc978 to your computer and use it in GitHub Desktop.
Save supergrass71/8d4b41af06e5c65145c26402be9cc978 to your computer and use it in GitHub Desktop.
[Cell Validation on the fly] Dynamically Add Data Validation to a Column of Cells #VBA #Excel
Sub AddValidation(cell as range)
'designed to work in tandem with worksheet function (see next)
If cell.rows.count>1 or cell.columns.count>1 then exit sub
With cell.validation
.delete
.add Type:=xlValidateList, AlertStyle:=xlValidateAlertStop, Operator: _
xlBetween, Formula:="A,list,of,your,values"
.ignoreblank = True
.incelldropdown = true
end with
end sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim v as integer
v=0
If not intersect(Target,Range("A;A")) is nothing then 'apply to your column
on error resume next
v = target.specialcells(xlcelltypesamevalidation).count
on error goto 0
If v= 0 then
call AddValidation(target)
end if
end if
end sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment