VBA复制粘贴公式search

下面的代码取自上一个问题

我的问题 – 我如何得到这个工作,如果查找和search值是公式? ..我试图改变.Value2,但似乎并没有工作。

VBA复制粘贴stringsearch

With Sheets("SheetName") ' Change to your actual sheet name Dim r As Range: Set r = .Range("C10:G10").Find(.Range("A10").Value2, , , xlWhole) If Not r Is Nothing Then r.Offset(4, 0).Resize(5).Value2 = .Range("A14:A18").Value2 

结束

在这里输入图像说明

如果您正在查找公式的结果,则需要为LookIn参数指定xlValues 。 当它为空时,默认为xlFormulas 。 例如,要find在“foo”表上产生“Bar”(即=CONCATENATE("B","a","r") )的=CONCATENATE("B","a","r") ,你可以这样做:

 With ActiveWorkbook.Sheets("Foo") Dim r As Range Set r = .UsedRange.Find("Bar", , xlValues, xlWhole) If Not r Is Nothing Then Debug.Print r.Address End If End With 

如果您想查找包含实际公式的工作表,则可以完全忽略LookIn参数或明确指定它:

 With ActiveWorkbook.Sheets("Foo") Dim r As Range Set r = .UsedRange.Find("=CONCATENATE(""B"",""a"",""r"")", , _ xlFormulas, xlWhole) If Not r Is Nothing Then Debug.Print r.Address End If End With