如何实现数据有效性列表项同步更新?
时间:2022-10-12 07:12:32
Q:如图1所示,在单元格C1中设置了数据有效性,列表数据来源于单元格区域A1:A3。这里,将单元格区域A1:A3命名为testData?
图1
然而,当我修改了单元格区域A1:A3中的数据后,单元格C1中的数据不会自动相应修改,如图2所示。
图2
如何实现数据源中的数据修改后,设置了数据有效性的单元格中的数据相应地自动修改?
A:下面使用工作表的Change事件来实现,代码如下:
Private SubWorksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim rngFound As Range
‘当名称为testData的区域数据改变时
If Not Intersect(Target,Me.Range(“testData”)) Is Nothing Then
‘遍历工作表中的数据有效性单元格
For Each rng InMe.Cells.SpecialCells(xlCellTypeAllValidation).Cells
‘如果单元格中的数据有效性设置为区域testData
If rng.Validation.Formula1 =”=testData” Then
‘检查该单元格中的值是否在区域testData列表值中
Set rngFound =Me.Range(“testData”).Find(rng.Value, , xlValues, xlWhole)
‘如果值不在列表中,命名区域中的数据一定被修改了,因此单元格值进行相应的修改
If rngFound Is Nothing Then
Application.EnableEvents =False
rng.Value = Target.Value
Application.EnableEvents =True
End If
End If
Next rng
End If
End Sub
运行后的效果如下图3所示。
图3
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
图片放在文件夹里,EXCEL也能查看
![](https://img.aspxhome.com/file/2023/5/a142105_0s.gif)
英特尔驱动造成Win10 21H1蓝屏死机怎么办?
![](https://img.aspxhome.com/file/2023/7/51497_0s.jpg)
word或csv文件转换成excel的方法
iOS 15.5钱包App如何添加 Apple 账户?
![](https://img.aspxhome.com/file/2023/2/45402_0s.png)
Word如何设置文字效果转换弯曲?Word设置文字效果转换弯曲方法
![](https://img.aspxhome.com/file/2023/1/30801_0s.jpg)
Win11原版ISO镜像下载_Win11官方ISO镜像下载[64位专业版]
![](https://img.aspxhome.com/file/2023/2/53352_0s.jpg)
WPS2016阅读进度如何快速调出
![](https://img.aspxhome.com/file/2023/6/a163756_0s.jpg)
Office 2019专业增强版与Visio 2016不能共存的解决办法
Word文档数据升序和降序怎么切换?Word文档数据升序和降序教学
![](https://img.aspxhome.com/file/2023/0/31120_0s.png)
Word自带的截图功能如何使用?
![](https://img.aspxhome.com/file/2023/7/25367_0s.png)
Word2013中如何去掉尾注上的一点短的横线?
![](https://img.aspxhome.com/file/2023/1/27681_0s.jpg)
Word2007鼠标右键点击语法错误字词下面的红色波浪线再单击"语法",Word崩溃
![](https://img.aspxhome.com/file/2023/4/21124_0s.jpg)
Win10系统组策略怎么打开?Win10系统组策略打开方法
![](https://img.aspxhome.com/file/2023/1/48751_0s.jpg)
excel2013怎么制作地图
![](https://img.aspxhome.com/file/2023/5/36425_0s.jpg)
在Word中怎样给表格的数据排序
wps双十一红包广告怎么屏蔽
![](https://img.aspxhome.com/file/2023/0/53570_0s.jpg)
win10远程连接win7连接不上去怎么办?这有解决的方法么?
![](https://img.aspxhome.com/file/2023/5/47335_0s.jpg)
word页眉两端对齐如何设置?
![](https://img.aspxhome.com/file/2023/0/18850_0s.png)
Word文档怎么限制编辑
Excel2010如何填充日期?
![](https://img.aspxhome.com/file/2023/4/36524_0s.jpg)