繁体   English   中英

Excel VBA-当某些名称与另一本工作簿中的某些名称匹配时,复制并粘贴一系列单元格

[英]Excel VBA - Copy and paste a range of cells when certain names match from one work book another

我正在做一个项目,我想将某些数据从一张纸复制到另一张纸。 例如,如果在"A"列中某个单元格包含"Hello"则复制单元格“ E4”中的内容。 “ IF”语句会起作用吗?

到目前为止,我的项目代码是

Sub testfortito()
    Dim x As Workbook
    Dim y As Workbook
    Dim ws As Worksheet

    'this opens both workbooks
    Set x = Workbooks.Open("Location1")
    Set y = Workbooks.Open("Location2")

    'to do the copy
    x.Sheets("sheet3").Range("A2:AC2").Copy

    For Each ws In Worksheets
        If ws.Name <> "sheet3" Then
            ws.Range("E3").Copy
            Worksheets ("Sheet3")
        End If    
    Next ws
End Sub

尝试这个

Sub testfortito()
    Dim colEtxt(), ctr

    ctr = 0
    With Sheet1.Range("A:A")
        Set txt = .Find(What:="hello", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not txt Is Nothing Then
            firstAddress = txt.Address
            Do
                ReDim Preserve colEtxt(ctr)
                colEtxt(ctr) = Range(txt.Address).Offset(0, 4).Value
                Set txt = .FindNext(txt)
                ctr = ctr + 1
            Loop While Not txt Is Nothing And txt.Address <> firstAddress
        End If
    End With

    'copy the array onto sheet2 using transpose technique
    Sheet2.Range("A1:A" & UBound(colEtxt) + 1) = WorksheetFunction.Transpose(colEtxt)
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM