如何替换excel中的函数
我有一个巨大的excel文件,里面有很多公式。在单元格中有一堆对旧函数funcA的引用,它将采用1个参数。该参数还可以嵌入其他函数、其他单元格值或公式。像这样如何替换excel中的函数,excel,excel-formula,Excel,Excel Formula,我有一个巨大的excel文件,里面有很多公式。在单元格中有一堆对旧函数funcA的引用,它将采用1个参数。该参数还可以嵌入其他函数、其他单元格值或公式。像这样 =C1+C2+funcA(C3)/2 =funcA(C4+AnotherFun(B1)) 现在我需要把所有的funcA都改成funcB。然而,funcB有两个参数。我需要funcA的原始参数作为funcB的第一个参数,但第二个参数为0。所以在更换之后,它看起来是这样的 =C1+C2+funcB(C3,0)/2 =funcB(C4+A
=C1+C2+funcA(C3)/2
=funcA(C4+AnotherFun(B1))
现在我需要把所有的funcA都改成funcB。然而,funcB有两个参数。我需要funcA的原始参数作为funcB的第一个参数,但第二个参数为0。所以在更换之后,它看起来是这样的
=C1+C2+funcB(C3,0)/2
=funcB(C4+AnotherFun(B1),0)
当我尝试替换funcA->funcB时,excel拒绝了,因为funcB需要两个参数。此外,我还需要考虑在函数调用中添加一个',0'的方法。我想到了RegEx match,但Excel似乎不支持这一点
我能做什么?为什么不创建FuncB并让FuncA调用B传入0和第一个参数?将是最简单的修复方法-假设返回类型为sameI无法更改FuncA。。。。它在外接程序中,我正在尝试摆脱该外接程序。哇。。。。我们必须采用这个复杂的解决方案。我明天上班时会检查一下。谢谢!这不适用于嵌套的funcA函数;e、 g.=funcafuncaa,但这很容易用InStrRev循环解决。我遇到了一个问题,因为公式是数组的一部分@用户11495492
Option Explicit
Sub ReplaceFunction()
Dim ufr As Range, ufrng As Range
Dim b As Long, i As Long, x As Long, f As String
Dim oldf As String, newf As String, p As String
oldf = "funcA("
newf = "funcB("
p = ", 0)"
On Error Resume Next
Set ufrng = Worksheets("sheet3").Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not ufrng Is Nothing Then
For Each ufr In ufrng
f = ufr.Formula
b = 0
x = InStr(1, f, oldf, vbTextCompare)
If x > 0 Then
For i = x + Len(oldf) To Len(f)
'are there nested functions?
If Mid(f, i, 1) = ")" Then
b = b - 1
ElseIf Mid(f, i, 1) = "(" Then
b = b + 1
End If
'ending bracket for funcA
If b = -1 Then
'add parameter
f = Application.Replace(f, i, 1, p)
'change function
f = Replace(expression:=f, Find:=oldf, Replace:=newf, compare:=vbTextCompare)
'no reason to continue
Exit For
End If
Next i
'change formula
ufr.Formula = f
End If
Next ufr
End If
End Sub
Function funcA(i As Integer)
funcA = i
End Function
Function funcB(i As Integer, j As Integer)
funcB = i * j
End Function
Function AnotherFunc(i As Integer)
AnotherFunc = i
End Function