2015-05-14 109 views
0

代碼問複製粘貼式搜索

我的問題 - 如何得到這個,如果查找和搜索值的公式可以正常工作? ..我試圖改變.Value2但似乎沒有工作。

VBA Copy Paste string search

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 

末隨着

enter image description here

回答

2

如果您正在尋找公式的結果,你需要爲找錢參數指定xlValues。當它爲空時,默認爲xlFormulas。如果你想找到包含實際公式您可以片

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 

:例如,要查找導致對錶「富」,「酒吧」(即=CONCATENATE("B","a","r"))的公式,你可以這樣做完全忽略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 
+0

您的代碼工作得非常好。我已將代碼更改爲複製forumlas, 作品發現期望我得到運行時錯誤'424' - 需要對象。我錯過了什麼?非常感謝 – Elixir

+0

使用表格(「Sheet1」)'更改爲您的實際工作表名Dim r As Range:Set r = .Range(「C10:G10」)。Find(.Range(「A10」)。Value2,,xlValues, xlWhole) 如果不是r沒有那麼r.Offset(4,0).Resize(5).PasteSpecial(xlPasteFormulas)= .Range(「A14:A18」)。複製 End With – Elixir

+0

@Elixir - 您不能設置'.Paste = .Copy'。複製移動到剪貼板,粘貼從它移動。將'.Range(「A14:A18」)。複製到一行,然後是'r.Offset(4,0).Resize(5).PasteSpecial(xlPasteFormulas)' – Comintern