Excel vba查找工作表中的文本,复制范围,粘贴到其他工作表

时间:2015-06-02 02:36:53

标签: excel vba

我是excel宏的新手,无法弄清楚如何解决我需要的东西。基本上我编写的代码在工作表中找到文本,当前位于B63("今天"),从找到的值中选择一行到下一个应停止的值,在这种情况下,B83("明天")。问题在于"今天"和"明天"每次下载新数据时都会上下移动。

我已尝试为此编写代码,但没有取得任何成功,此时我甚至不确定我是否采取了正确的方法。这就是我所拥有的,任何帮助将不胜感激:

Dim Cell As Range

    For i = 1 To 100
    For f = 30 To 100
    Sheets("Download").Select
    If Cells(i, "B").Value = "Today" Then
    If Cells(f, "B").Value = "Tomorrow" Then
    'Insert code to select rows from "i" to "f"-1 (one above f)
    Selection.Copy
    Sheets("Statements").Select
    Range("A3").Select
    ActiveSheet.Paste
    End If

    Next i

1 个答案:

答案 0 :(得分:0)

试试这个

Sub test()
Dim td As Range, tm As Range
With Sheets("Download")
    Set td = .[B:B].Find("today")
    Set tm = .[B:B].Find("tomorrow")
    If (Not td Is Nothing) And (Not tm Is Nothing) Then
        .Rows(td.Row & ":" & tm.Offset(-1, 0).Row).Copy Sheets("Statements").[A1]
    End If
End With
End Sub

<强>更新

试试这个

Sub test()
Dim td&, tm&, n&, cl As Range, Dwnld As Worksheet, Stmnt As Worksheet
td = 0: tm = 0
Set Dwnld = Sheets("Download"): Set Stmnt = Sheets("Statements")
    With Dwnld
    n = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
    For Each cl In .Range("B1:B" & n)
        If LCase(cl.Value) = "today" Then td = cl.Row
        If LCase(cl.Value) = "tomorrow" Then tm = cl.Offset(-1, 0).Row
        If td > 0 And tm > 0 Then
            .Rows(td & ":" & tm).Copy Stmnt.Range("A" & Stmnt.Cells(Rows.Count, "A").End(xlUp).Row + 1)
            td = 0: tm = 0
        End If
    Next cl
    End With
End Sub

enter image description here

目的地

enter image description here