如何以同样的方式一次修改多个公式?

时间:2017-05-24 16:29:06

标签: excel excel-vba vba

我确信这已经被问到并得到了回答,但是我无法以一种可以解决问题的方式来表达我的问题。

我想出了如何通过编辑这样的公式来过滤掉我收到的数据中的某些字符:

='Sheet1'!$B$4

变为

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE('Sheet1'!$B$4,"(",""),")",""),"N",""),"E",""),"S",""),"W",""),"m","")

这样做是从单元格中删除这六个字符中的任何一个。请注意,新公式包含原始公式。

我需要将此更改应用于工作表中的所有公式,并且有很多公式。我试过玩flash-fill;没运气。是否有更简单的方法来添加"那些替换所有公式的命令而不用手工编辑它们?

由于

编辑:这是我最终制作的宏,它起作用了:

Sub Macro1()
Dim CurrentCell As String
  CurrentCell = ActiveCell.Formula
  CurrentCell = Replace("=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(@,""("",""""),"")"",""""),""N"",""""),""E"",""""),""S"",""""),""W"",""""),""m"","""")", "@", Mid(CurrentCell, 2))
  ActiveCell.Formula = CurrentCell
End Sub

2 个答案:

答案 0 :(得分:0)

这是我最终制作的宏,它有效!

Sub Macro1()
Dim CurrentCell As String
  CurrentCell = ActiveCell.Formula
  CurrentCell = Replace("=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(@,""("",""""),"")"",""""),""N"",""""),""E"",""""),""S"",""""),""W"",""""),""m"","""")", "@", Mid(CurrentCell, 2))
  ActiveCell.Formula = CurrentCell
End Sub

答案 1 :(得分:0)

我相信,如果突出显示包含要更新的公式的单元格范围,然后编辑其中的第一个单元格(左上角),然后使用控制回车键,而不是直接输入,它将更新突出显示范围内的所有公式。