替换范围内单元格公式中的所有字符串实例?

时间:2014-11-21 22:56:15

标签: vba excel-vba excel

我有一个范围

Dim r as range

该范围内的单元格可以是可见的,也可以是隐藏的。

我想在公式中搜索$ B $ 2的所有实例,并用$ C $ 3替换它们。

这样做的好方法是什么?

2 个答案:

答案 0 :(得分:2)

尝试类似的事情,

Dim r as range, fw as string, rw as string
on error resume next
application.calculation = xlcalculationmanual
set r = Range("A1:Z99").SpecialCells(xlCellTypeFormulas, 23)
if not r is nothing then
    fw = "$B$2": rw = "$C$3"
    r.Replace what:=fw, replacement:=rw, lookat:=xlPart, matchcase:=False
    set r = nothing
end if
application.calculation = xlcalculationautomatic

编辑:确实不需要循环,所以我删除了它。

答案 1 :(得分:1)

以下适用于过滤器隐藏的单元格:

Sub test()

    Dim r As Range, fw As String, rw As String

    On Error Resume Next
    Set r = Range("A1:A5").SpecialCells(xlCellTypeFormulas)
    On Error GoTo 0

    If Not r Is Nothing Then
        fw = "$B$2": rw = "$C$3"
        For Each c In r.Cells
            c.Replace what:=fw, replacement:=rw, lookat:=xlPart, MatchCase:=False
        Next c
        Set r = Nothing
    End If

End Sub

我修改了Jeeped发布的解决方案