簡體   English   中英

Excel/VBA 查找字符串並復制偏移單元格

[英]Excel/VBA find string and copy offset cells

我需要一小塊 VBA 來執行以下操作,搜索特定字符串,例如(“新應用程序”),然后復制單元格偏移量 (0,2) 和 (0,5)。 因此,如果在 A34 中找到“新應用程序”,則需要復制 D34 和 J34……

任何幫助,非常感謝。

到目前為止我所擁有的如下,但我確定如何也復制偏移量(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

其余的代碼,重新粘貼等我已經有了,我只需要修改一小部分就可以了。

非常感謝

在這種情況下,使用變量會非常有幫助。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM