[英]Excel/VBA find string and copy offset cells
I'm in need of a small piece of VBA to do the following, search for a particular string, for instance ("New Applications"), and then copy the cells offset (0,2) and (0,5).我需要一小块 VBA 来执行以下操作,搜索特定字符串,例如(“新应用程序”),然后复制单元格偏移量 (0,2) 和 (0,5)。 Hence if "New Applications" is found in A34, I then need to copy D34 and J34…
因此,如果在 A34 中找到“新应用程序”,则需要复制 D34 和 J34……
Any help, much appreciated.任何帮助,非常感谢。
All I have so far is as below, but i'm sure how to also copy offset(0,5)..到目前为止我所拥有的如下,但我确定如何也复制偏移量(0,5)..
Sub test()
Cells.Find(What:="NB ASDA Applications", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(0, 2).Select
Selection.Copy
The rest of the code, re pasting etc I already have, I just need to amend a small part to something like above.其余的代码,重新粘贴等我已经有了,我只需要修改一小部分就可以了。
many thanks非常感谢
In this case it would greatly help to use a variable.在这种情况下,使用变量会非常有帮助。
Sub test()
'make a variable called foundrange that is of type "Range"
Dim foundRange as Range
'set this variable based on what is found: (note we remove the 'activate' here
Set foundRange = Cells.Find(What:="NB ASDA Applications", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False)
'You can copy now and do whatever you want. Say you want to copy these values to Sheet2!A1 and B1, respectively:
Sheet2.Range("A1").Value = foundRange.OFfset(0,2).value
Sheet2.Range("B1").Value = foundRange.Offset(0,5).value
'Or copy to the clipboard -- haven't tested this union, but I think it should work
Union(foundRange.Offset(0,2), foundRange.Offset(0,5)).Copy
'Or copy just one and do something
foundRange.Offset(0,2).Copy
'do something
'Copy the other one and do something
foundRange.Offset(0,5).Copy
'do something
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.