打开 Excel 工作表
在A1单元格中输入Orange
在A2单元格中输入Banana
在B1单元格中输入15
在B2单元格中输入23
转到开发人员选项卡,然后单击录制宏按钮
单击确定以录制宏。
一起选择A1和A2单元格
转至首页标签 > 条件格式> 新规则> 使用公式确定要设置格式的单元格
类型=B1>12
点击格式按钮
点击数字标签
点击自定义按钮
输入""@
点击确定按钮
再次点击确定按钮
转到开发人员选项卡,然后单击停止录制按钮
按 Alt+F11 查看以下宏。
Sub Macro1()
Range("A1:A2").Select
Selection.NumberFormat = """""@"
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=B1>12"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
ExecuteExcel4Macro "(2,1,""""""@"")"
Selection.FormatConditions(1).StopIfTrue = False
End Sub
当您运行录制的宏时,您将看到错误(运行时错误 1004)。
你知道如何解决运行时错误1004吗?
最佳答案
我做了一些研究,找到了你的解决方案 here宏记录器将恢复为旧的 ExecuteExcel4Macro 命令,以便设置条件格式规则的数字格式。这是没有必要的。
您的代码应如下所示:
Sub Macro1()
With ActiveSheet.Range("A1:A2")
.FormatConditions.Delete 'Removes all the rules from the selection first
.FormatConditions.Add Type:=xlExpression, Formula1:="=B1>12"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).NumberFormat = """""@"
.FormatConditions(1).StopIfTrue = False
End With
End Sub
关于excel - 条件格式宏给我运行时错误 1004 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66018679/