Forum Discussion
Data Validation list value update after data list edit.
- Feb 18, 2023
This requires VBA code, with some side effects:
- Users will have to allow macros.
- It won't work in Excel Online, nor on iOS or Android.
- If you change a cell in the list, Undo will be disabled.
- After changing a cell in the list, Excel will jump back to that cell.
In the attached demo workbook, I have named the source range List, and I have selected all cells that have data validation based on this range, and assigned that range the name DVCells.
I right-clicked the sheet tab and selected 'View Code'.
I copied the following code into the worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range) Dim Src As Range Dim OldVal, NewVal If Intersect(Range("List"), Target) Is Nothing Then Exit Sub Application.ScreenUpdating = False Application.EnableEvents = False If Intersect(Range("List"), Target).Count > 1 Then MsgBox "Do not change multiple cells in the list at once!", vbCritical Application.Undo GoTo ExitHere End If Set src=Intersect(Range("List"), Target) NewVal = Src.Value Application.Undo OldVal = Src.Value Src.Value = NewVal If OldVal <> "" Then Range("DVCells").Replace What:=OldVal, Replacement:=NewVal, LookAt:=xlWhole End If ExitHere: Application.EnableEvents = True Application.ScreenUpdating = True End SubI switched back to Excel and saved the workbook as a macro-enabled workbook (*.xlsm).
Hi , your suggestions are great.
I'm new to VBA .
I can get your demo working if all the lists are on one worksheet.
How does one adopt it to cover the situation where:
- the list is on one sheet
- the DV values are on multiple other sheets?
I have tried placing your code on:
- the worksheet containg the list
- all worksheets
- the workbook
I'm sure it's a newbie question but your help would be appreciated.
Regards
The code should now be in the worksheet module of each of the sheets containing the lists, NOT in the module of the sheet containing the data validation.
And you have to refer to the data validation ranges in such a way that the code knows where to find them.
In the attached workbook, I created a common procedure named UpdateDV in a standard module. This is called from the Worksheet_Change event procedures of each of the list sheets, to avoid having to write almost identical code for each sheet.
See Formulas > Name Manager for the definition of the named ranges I used, and see the Visual Basic Editor for the code.
- ross111275Apr 04, 2024Copper ContributorThanks for your help
One issue I can't resolve.
When I have one primary list (List 1) and I want it to change multiple Data Validation Lists (DVCells2 and DVCells3 etc.)on different pages - why can't I simply place multiple calls to DVUpdate in the page where the primary list exists
e.g.:
Private Sub Worksheet_Change(ByVal Target As Range)
Call UpdateDV("List1", "DVCells2", Target)
Call UpdateDV("List1", "DVCells3", Target)
End Sub
Debug says it hangs on Application.Undo
Sorry to bother youa gain. MAny thanks
Ross- HansVogelaarApr 04, 2024MVP
Since you asked for different validation ranges for each list, I didn't take into account that you'd have multiple validation ranges with the same source list. The code has to be modified again...
- ross111275Apr 05, 2024Copper ContributorMany thanks. I'm sure many will benefit from your work. Much appreciated.
All the best
- ross111275Apr 03, 2024Copper Contributor
Excellent, Much appreciated!!
I thought it must have been a scoping issue but due to my lack of familiarity with VBA I didn't know how to approach the problem.
Regards
Ross