Excel一键实现单元格变成性别下拉菜单选项技巧
1、绘制控制按钮,本教材以插入一个矩形形状为例。
2、编辑文字(目的是便于理解),本教材编辑文字为:男/女。
3、美化按钮,通过形状填充,轮廓填充,形状效果等设置。
4、复制该形状,编辑文字为清除。PS:此形状的作用是将设置好的男/女下拉菜单清除。
5、从excel窗口切换到VBA窗口,具体操作方法可参考一下链接:
6、插入模块,复制粘贴以下代码:Option ExplicitDim rng As RangeSub boy_or_girl() '性别选择Set rng = Selection With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:="男,女" .IgnoreBlank = True .InCellDropdown = True .InputTitle = "" .ErrorTitle = "" .InputMessage = "" .ErrorMessage = "" .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End WithEnd SubSub Clear() '清除下拉菜单Set rng = Selection With Selection.Validation .Delete .Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator:=xlBetween .IgnoreBlank = True .InCellDropdown = True .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End WithEnd Sub
7、返回Excel界面,为两个按钮指定不同的宏,”男/女“按钮指定性别选择代码,”清除“按钮选择清除下拉菜单代码。下图只展示了”男/女“按钮的指定方法,”清除“按钮指定方法类似。
8、选择需要生成性别输入的单元格,点击”男/女“按钮即可,若要清除,方法一样,选择区域,点击”清除“按钮即可。本教材以B列为例。