应用实例13 动态数据有效性 省份 | 市名 | 县区名 | 邮编 | 天津市 | | | 120000 | | | | | 江西省 | 江西省新余市 | | 360500 | | | | | | | | | | | | | | | | | 河北省 | 河北省保定市 | 河北省保定市郊区 | 130605 | | | | |
'模块代码 Public d As New Dictionary '定义字典对象 Sub createvaldition() On Error Resume Next '忽略错误 Dim arr, i As Long arr = Sheets("代码表").[a1].CurrentRegion d.Add "all", "" '省市 For i = 1 To UBound(arr) '遍历 d.Add arr(i, 1), arr(i, 2) '地名查邮编 d.Add arr(i, 2), arr(i, 1) '邮编查地名 If arr(i, 2) Like "##0000" Then d("all") = d("all") & "," & arr(i, 1) '省级 If Mid(arr(i, 2), 3) > "0000" Then '省级以下 If Right(arr(i, 2), 2) = "00" Then '地市级 Mid(arr(i, 2), 3, 4) = "0000" d(d(arr(i, 2))) = d(d(arr(i, 2))) & "," & arr(i, 1) '嵌套字典对象,反查 Else Mid(arr(i, 2), 5, 2) = "00" '县区级 d(d(arr(i, 2))) = d(d(arr(i, 2))) & "," & arr(i, 1) End If End If Next With [a2:a40].Validation '设置数据有效性 .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Mid(d("all"), 2) '省份名称 .IgnoreBlank = True .InCellDropdown = True .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End With End Sub
'工作表代码 Private Sub Worksheet_Change(ByVal Target As Range) On Error Resume Next '忽略错误 If Target.Column < 4 Then '前三列 If Len(Target.Text) > 0 Then Cells(Target.Row, 4) = Left(d(Target.Value), 6) '不为空使用字典取其邮编置于第四列 Else Target.Offset(, 1).Resize(1, 3) = "" '为空则删除右面单元格的内容 Cells(Target.Row, 4) = Left(d(Target.Offset(, -1).Value), 6) '取相邻左面单元格地名的邮编 End If With Target.Offset(, 1).Validation '设置右面单元格的数据有效性 .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=Mid(d(Target.Value), 8) .IgnoreBlank = True .InCellDropdown = True .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End With End If End Sub
33dSFbcl.rar
(105.43 KB, 下载次数: 2165)
[此贴子已经被作者于2007-9-10 23:09:20编辑过] |