参考代码如下(请确保在工作表中添加的按钮为表单控件,并修改每一个按钮的文字为所要替换的内容):
Public Sub ChangeFormula(ByVal Column As Variant) Dim varCaller As Variant On Error Resume Next varCaller = Application.Caller If Err.Number = 0 Then Dim strFind As String Dim strReplace As String Dim strMonth As String Dim rngColumn As Range Dim rngFormula As Range strMonth = ChrW$(&H6708) strReplace = "]" & ActiveSheet.Buttons(varCaller).Caption strFind = "]??" & strMonth Set rngColumn = ActiveSheet.Columns(Column) Set rngFormula = rngColumn.Find(strFind, , xlFormulas, xlPart) If rngFormula Is Nothing Then strFind = "]?" & strMonth Set rngFormula = rngColumn.Find(strFind, , xlFormulas, xlPart) End If If Not (rngFormula Is Nothing) Then rngColumn.Replace strFind, strReplace End If Set rngFormula = Nothing Set rngColumn = Nothing End IfEnd Sub
然后右击按钮选择指定宏并输入宏名:
'ChangeFormula 3'
或
'ChangeFormula "C"'