Apply and Remove Excel Data Validation Automatically with VBA
Question details
Automatically add a payee dropdown when a bank payment ID is entered, then remove it and split the selected payee into six text columns.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet contains a bank payment table and a named list of hyphen-delimited payee details.
- Observed behavior
- Entering a payment ID should add validation to a blank payee cell. Selecting a payee should split its details, remove validation, and adjust the column width.
Confirm the table is named BankPymtDet, its columns are named Bank Payment ID and Name of Payee, and the payee column is immediately to the right of the ID column. Define a workbook-level named range called PayeeDetails and ensure each list entry has six hyphen-separated fields; the payee cell and five cells to its right must be available for the split.
Use a Worksheet_Change macro to manage payee validation
Add the event procedure to the worksheet containing the payment table to handle individual cell changes automatically.
The macro processes one changed cell at a time. It adds the dropdown only when the adjacent payee cell is blank, and formats all six split fields as text to preserve values such as leading zeros.
Text to Columns writes into the payee cell and the next five cells, so make sure those cells can be overwritten. The macro disables alerts while splitting.
Right-click the worksheet tab that contains BankPymtDet and select View Code.
Paste the following code into the worksheet module. If it already contains a Worksheet_Change procedure, integrate the logic into that procedure instead of adding a second one: Private Sub Worksheet_Change(ByVal Target As Range) Dim idCell As Range, payeeCell As Range Dim oldEvents As Boolean, oldScreen As Boolean, oldAlerts As Boolean Dim errorText As String If Target.CountLarge > 1 Then Exit Sub oldEvents = Application.EnableEvents oldScreen = Application.ScreenUpdating oldAlerts = Application.DisplayAlerts On Error GoTo CleanFail Set idCell = Intersect(Target, Me.ListObjects("BankPymtDet").ListColumns("Bank Payment ID").DataBodyRange) Set payeeCell = Intersect(Target, Me.ListObjects("BankPymtDet").ListColumns("Name of Payee").DataBodyRange) If Not idCell Is Nothing Then If idCell.Value <> "" And idCell.Offset(0, 1).Value = "" Then Application.EnableEvents = False With idCell.Offset(0, 1) .Validation.Delete .Validation.Add Type:=xlValidateList, Formula1:="=PayeeDetails" .ColumnWidth = 60 End With End If ElseIf Not payeeCell Is Nothing Then If payeeCell.Value <> "" Then Application.EnableEvents = False Application.ScreenUpdating = False Application.DisplayAlerts = False payeeCell.Validation.Delete payeeCell.TextToColumns Destination:=payeeCell, DataType:=xlDelimited, _ TextQualifier:=xlTextQualifierNone, ConsecutiveDelimiter:=False, _ Tab:=False, Semicolon:=False, Comma:=False, Space:=False, _ Other:=True, OtherChar:="-", _ FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2), _ Array(4, 2), Array(5, 2), Array(6, 2)) payeeCell.EntireColumn.AutoFit End If End If CleanUp: On Error Resume Next Application.DisplayAlerts = oldAlerts Application.ScreenUpdating = oldScreen Application.EnableEvents = oldEvents On Error GoTo 0 If errorText <> "" Then MsgBox errorText, vbExclamation, "Payee validation" Exit Sub CleanFail: errorText = Err.Description Resume CleanUp End Sub
Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). When reopening the file, enable macros only if you trust the workbook.
Enter an ID in a single Bank Payment ID cell, select a value from the adjacent payee dropdown, and verify that six text fields appear and the payee column is autofitted.

Work with payment spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility for an easy move between apps. Use Microsoft Excel for the VBA event workflow described above; WPS does not fix or guarantee compatibility with that macro.

Frequently Asked Questions
Why does the payee dropdown not appear after I enter an ID?
Check that macros are enabled, the code is in the worksheet's View Code module, and the table and column names match the code. The adjacent payee cell must also be blank, and the change must affect only one cell.
How do I create the PayeeDetails list?
Put the payee entries in a range, select the range, and use Formulas > Define Name to create the workbook-level name PayeeDetails. Each entry should contain six fields separated by hyphens.
Will the split preserve leading zeros?
The macro specifies text format for all six fields in Text to Columns, which helps preserve leading zeros in the selected value. Make sure the source list entries already contain those zeros.
Does the macro restore the original payee column width?
No. It sets the width to 60 when adding the dropdown, then uses AutoFit after the selection. AutoFit sizes the column to its contents rather than restoring its previous width.
What happens if I paste several payment IDs at once?
The procedure exits when more than one cell changes. Enter IDs one cell at a time, or modify the event procedure to process multiple cells.




