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).
In my previous reply, I wrote
"1) The code should only be in the worksheet module of the Setting sheet, NOT in the worksheet module of the EMPTY sheet."
You still have code in the worksheet module of the EMPTY sheet. You should delete that code. It causes the error you mention.
You only need the code in the Setting sheet.
As far I can tell that is exactly what I have done. I have pasted the code in the worksheet module of the Setting sheet. I've attached an image illustrating the steps I'm making, hopefully this sheds light on where I went wrong along the process.
P.s: the excel file is a Macro Enabled Worksheet
- HanyBaghdadyFeb 23, 2023Copper Contributor
- HansVogelaarFeb 22, 2023MVP
Activate the EMPTY sheet.
Select 'View Code' from the context menu.
Delete all the code you see there.
Close the Visual Basic Editor.
Save the workbook.