【问题标题】:VBA Copy Paste formula searchVBA 复制粘贴公式搜索
【发布时间】:2015-05-14 21:32:53
【问题描述】:

以下代码取自上一个问题

我的问题 - 如果查找和搜索值是公式,我如何让它工作? ..我尝试更改 .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

结束

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    如果您要查找公式的结果,则需要为 LookIn 参数指定 xlValues。当它为空时,默认为xlFormulas。例如,要在工作表“foo”上查找任何导致“Bar”(即=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
    

    【讨论】:

    • 您的代码运行良好。我已将代码更改为复制 forumlas,工作发现期望我收到“424”的运行时错误 - 需要对象。我错过了什么?非常感谢
    • With Sheets("Sheet1") ' 更改为您的实际工作表名称 Dim r As Range: Set r = .Range("C10:G10").Find(.Range("A10"). Value2, , xlValues, xlWhole) If Not r Is Nothing Then r.Offset(4, 0).Resize(5).PasteSpecial(xlPasteFormulas) =​​ .Range("A14:A18").Copy End With
    • @Elixir - 你不能设置.Paste = .Copy。复制移动到剪贴板,粘贴移动。将.Range("A14:A18").Copy 放在一行,然后是r.Offset(4, 0).Resize(5).PasteSpecial(xlPasteFormulas)
    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多