excel选择下拉框实现复选
第一步:新建一个excel且设置数据有效性【选中x列--数据--有效性】
第二步:开发工具--查看代码--把代码复制进去保存就ok了
代码如下:
private sub worksheet_change(byval target as range)' developed by contextures inc.' www.contextures.comdim rngdv as rangedim oldval as stringdim newval as stringif target.count > 1 then goto exithandler on error resume nextset rngdv = cells.specialcells(xlcelltypeallvalidation)on error goto exithandler if rngdv is nothing then goto exithandler if intersect(target, rngdv) is nothing then 'do nothingelse application.enableevents = false newval = target.value application.undo oldval = target.value target.value = newval if target.column = 7 then '这里规定好哪一列的数据有效性是多选的,a列是第1列,依次类推,如3就是c列,7就是g列 if oldval = "" then 'do nothing else if newval = "" then 'do nothing else if instr(1, oldval, newval) <> 0 then '重复选择视同删除 if instr(1, oldval, newval) + len(newval) - 1 = len(oldval) then '最后一个选项重复 target.value = left(oldval, len(oldval) - len(newval) - 1) else target.value = replace(oldval, newval & ",", "") '不是最后一个选项重复的时候处理逗号 end if else '不是重复选项就视同增加选项 target.value = oldval & "," & newval' note: you can use a line break,' instead of a comma' target.value = oldval _' & chr(10) & newval end if end if end if end ifend if exithandler: application.enableevents = trueend sub
更多excel相关技术文章,请访问excel基础教程栏目!
以上就是excel如何实现下拉框复选的详细内容。
