VBA复制粘贴字符串搜索 [英] VBA Copy Paste string search

查看:62
本文介绍了VBA复制粘贴字符串搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何编写一个通过单元格C10:G10搜索以找到等于单元格A10的匹配项的vba代码,一旦找到该匹配项,就将范围A14:A18复制到匹配的单元格中,但在例如F14之下:F18(查看图片)

I can’t seem to figure out how to write a vba code that search’s through cells C10:G10 to find a match that equals cell A10, once found, copies range A14:A18 to the matched cell but below e.g F14:F18 (See Image)

下面的宏

'Copy
Range("A14:A18").Select
Selection.Copy
'Paste
Range("F14:F18").Select
ActiveSheet.Paste!

推荐答案

尝试一下:

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
End With

范围对象具有 查找方法 ,以帮助您查找范围内的值.
然后返回与您的搜索条件匹配的Range对象.
要使值正确定位,只需使用 Offset and Resize Method .

Edit1 :要回答OP的评论

要在范围中查找公式,您需要将 LookIn 参数设置为 xlFormulas .

To find formulas in Ranges, you need to set LookIn argument to xlFormulas.

Set r = .Range("C10:G10").Find(What:=.Range("A10").Formula, _
                               LookIn:=xlFormulas, _
                               LookAt:=xlWhole)

上面的代码查找范围与单元格A10完全相同的范围.

Above code find Ranges with exactly the same formula as Cell A10.

这篇关于VBA复制粘贴字符串搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆