使用“包含”条件移动行

时间:2019-03-04 23:36:47

标签: excel vba

我已经建立了一个宏,用于根据特定条件将信息行移动到新工作表中。

所有这些都起作用,但是最后一个需要拉出任何“包含”文本的内容。

这是我写的,但是没有用。帮助吗?

当前代码:

Dim r As Range
Dim i As Integer
Dim Source As Worksheet
Dim Target As Worksheet
Set Source = ActiveWorkbook.Worksheets("Sheet1")
Set Target = ActiveWorkbook.Worksheets("Sheet2")
i = 2
For Each r In Source.Range("E1:E3000")
    If r = "=*Wavelengths*" Then
        Source.rows(r.Row).Cut Target.rows(i)
        i = i + 1
    End If
Next r

2 个答案:

答案 0 :(得分:0)

使用instr函数返回字符串在另一个字符串中的位置。检查代码中的注释以查找行为说明。

进一步参考: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/instr-function

尝试以下代码:

Select distinct a.author_ID as Focal, b.author_id as Co
from MyTable a
left join MyTable b
on a.book_id = b.book_id
and a.author_id <> b.author_id
order by Focal, Co

答案 1 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    With ThisWorkbook

        Dim rng As Range, cell As Range
        Dim i As Long
        Dim Source As Worksheet, Target As Worksheet

        Set Source = .Worksheets("Sheet1")
        Set Target = .Worksheets("Sheet2")
        Set rng = Source.Range("E1:E3000")

        i = 2

        For Each cell In rng

            If InStr(1, cell.Value, "Wavelengths") > 0 Then

                Source.Rows(cell.Row).Cut Target.Rows(i)
                i = i + 1

            End If

        Next cell

    End With

End Sub

注意:

如果要在剪切后删除空行-粘贴,则应使用以下方法将行从下往上循环:

For i=Lastrow to ... step -1
Next i