查找,替换和公式

时间:2014-11-13 12:37:43

标签: excel-vba replace excel-formula vba excel

我想在特定列(例如Apple)中查找单词的所有实例并替换内容(例如Banana),然后在已保存值的不同列中的同一行上(例如50) )插入一个公式来计算该值的95%(例如= 50 * 95%)。

我录制了宏并实现了这个目标:

Sub Apple50toBanana95()

    ' Find Apple
    ActiveSheet.Range("$A$1:$AV$1600").AutoFilter Field:=1, Criteria1:= _
        "Apple"
    Range("A2").Select
    ' Replace Apple with Banana
    ActiveCell.FormulaR1C1 = "Banana"
    Range("A2").Select
    'Calculate 95% of the value
    ActiveCell.FormulaR1C1 = "=50*95%"
    Range("B2").Select

End Sub

它只会在第二行的第一个记录中运行。有没有办法将它应用于所有实例?

1 个答案:

答案 0 :(得分:2)

查看此帮助:http://msdn.microsoft.com/en-us/library/office/ff839746(v=office.15).aspx

With Worksheets(1).Range("a1:a500") ' Set your search range
    Set c = .Find("Apple", lookin:=xlValues)
    If Not c Is Nothing Then 
        firstAddress = c.Address 
        Do 
            c.Value = "Banana" ' Replace value
            'add further operations using excel offset feature like c.Offset(1, 1).value = something

            Set c = .FindNext(c) 
        Loop While Not c Is Nothing And c.Address <> firstAddress 
    End If 
End With