excel查找副本,粘贴所有以1Z开头的值

时间:2016-04-17 14:36:28

标签: excel excel-vba vba

我正在尝试找到一个脚本,它在sheet1中找到某些值并将这些值粘贴到sheet2 A1中。

  1. 范围是整张1
  2. 需要搜索所有以" 1Z"和" W"
  3. 粘贴以" 1Z"开头的每个单元格数据;和" W"在第A行下的sheet2中。
  4. 目前有这个脚本:

    Sub delete_oldads()
    Dim cel As Range, cfind As Range
    ActiveSheet.UsedRange.Select
    For Each cel In Selection
    If cel = "" Then GoTo nextcel
    Set cfind = cel.Find(what:="1Z", lookat:=xlPart)
    If Not cfind Is Nothing Then
    
    cfind.Copy Cells(cfind.Row, "A")
    cfind.Clear
    End If
    nextcel:
    Next cel
    End Sub
    

    但是这一个在同一张纸上复制/粘贴所有匹配的单元格,如果在同一行中找到匹配,它将只复制最后一个。

1 个答案:

答案 0 :(得分:0)

这不使用 FIND(),可能有点慢:

Sub poiuyt()
Dim K As Long, r As Range
Dim sh2 As Worksheet

Set sh2 = Sheets("Sheet2")
K = 1

With Sheets("Sheet1")
    For Each r In .UsedRange
        v = r.Value
        If v <> "" Then
            If Left(v, 1) = "W" Or Left(v, 2) = "IZ" Then
                r.Copy sh2.Cells(K, 1)
                K = K + 1
            End If
        End If
    Next r
End With
End Sub