简体   繁体   English

在公式单元格上使用 Range.Replace

[英]Using Range.Replace on Formula Cells

I have a short list of values from A1 through A10 :我有一个从A1A10的简短值列表:

在此处输入图片说明

A4 contains the string ab and A5 contains the formula: A4包含字符串abA5包含公式:

="a" & "b"

If I run:如果我运行:

Sub Test1()
    Dim r As Range

    Set r = Range("A1:A10")
    r.Replace What:="ab", Replacement:="x"
End Sub

only A4 gets modified.只有A4被修改。

How can I get the Replace Method to work for both cases ??我怎样才能让替换方法适用于这两种情况?

EDIT#1:编辑#1:

I can always use a loop to examine/replace item-by-item, but .Replace is much faster.我总是可以使用循环逐项检查/替换,但.Replace速度要快得多。 I suppose that I could build and use a temporary AutoFilter, but this seems extreme.我想我可以构建和使用一个临时的 AutoFilter,但这似乎是极端的。

This is what I have created:这是我创建的:

Option Explicit

Sub TestMe()

    Dim myCell As Range
    Dim myText As String

    For Each myCell In Worksheets(1).Range("A1:A10")
        If InStr(myCell.Text, "ab") > 0 Then
            myText = myCell.Text
            myCell = Replace(myText, "ab", "x")
            myCell.Value = myText
        End If
    Next myCell

End Sub

Interesting problem, it seems that a LookIn option would be nice.有趣的问题,似乎LookIn选项会很好。

I tried a few things (all using loops however).我尝试了一些东西(但是都使用循环)。 I disabled screenupdating for all the tests.我禁用了所有测试的屏幕更新。

Using Replace :使用Replace

r.Replace what:="ab", Replacement:="x"

using Find :使用Find

Dim c As Range
Set c = r.Find(what:="ab", LookIn:=xlValues)
While Not (c Is Nothing)
    c.Value = "x"
    Set c = r.FindNext
Wend

using a simple loop:使用一个简单的循环:

Dim i As Long
For i = 1 To 10
    If Cells(i, 1).Value = "ab" Then Cells(i, 1).Value = "x"
Next i

Using a better loop:使用更好的循环:

Dim c as Range
    For Each c In r.Cells
    If c.Value = "ab" Then c.Value = "x"
Next c

Using an array to search:使用数组进行搜索:

Dim v As Variant
Dim i as Long
v = r.Value
For i = 1 to 10
    If v(i,1) = "ab" Then Cells(i,1).Value = "x"
next i

The Replace and array method were the fastest to search the range and I didn't notice any speed difference. Replace和数组方法是搜索范围最快的方法,我没有注意到任何速度差异。 However, the writing to the cells slowed down the loops considerably when there were many replacements to do (it got noticeable somewhere around 5000 replacements out of 1,000,000 values for me).然而,当有很多替换要做时,写入单元格会大大减慢循环的速度(对我来说,在 1,000,000 个值中大约有 5000 个替换值)。 The Find suffered heavy from more replacements and the other two loops were much slower when searching. Find受到更多替换的影响,其他两个循环在搜索时要慢得多。

Conclusion: Using an internal array is the best way (that I can think of. It even beats removing all the formulas first ( r.Value = r.Value ).结论:使用内部数组是最好的方法(我能想到的。它甚至胜过首先删除所有公式( r.Value = r.Value )。

Maybe saving all the occurrences and replacing them after the loop could speed things up further.也许保存所有事件并在循环后替换它们可以进一步加快速度。

also you can use this:你也可以使用这个:

Sub ThereIsAnotherOneVariant()
    With [A1:A10]
        .Value2 = .Value2
        .Replace "ab", "x"
    End With
End Sub

but this variant will remove all formulas in the range但此变体将删除范围内的所有公式

Even if this is a historical thread, I think it is good to emphasize that fastest method ( Replace ), works very well for solving the question in discussion, without any iteration.即使这是一个历史线索,我认为最好强调最快的方法 ( Replace ),无需任何迭代即可很好地解决讨论中的问题。 In fact, range 'Replace' method works Only for string formulas... But due to the fact that Formula property of a range without formula returns its text, it works also in that case, too.事实上,范围“替换”方法适用于字符串公式......但由于没有公式的范围的Formula属性返回其文本,它也适用于这种情况。 It only searches and replaces twice, but it needs the exact formula string like What parameter:它只搜索和替换两次,但它需要确切的公式字符串,如What参数:

Sub Test1_Bis()
    Dim r As Range

    Set r = Range("A1:A10")
    r.Replace What:="=""a"" & ""b""", Replacement:="x"
    r.Replace What:="ab", Replacement:="x"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM