【问题标题】:Excel vba find text in sheet, copy range, paste to other sheetExcel vba 在工作表中查找文本,复制范围,粘贴到其他工作表
【发布时间】:2015-06-02 02:36:53
【问题描述】:

我对 excel 宏比较陌生,不知道如何解决我需要的问题。基本上我正在编写一个代码,它在当前位于 B63(“今天”)的工作表中查找文本,从找到的值向下选择行到它应该停止的下一个值,B83(“明天”)在这个案例。问题是每次下载新数据时,“今天”和“明天”都会上下移动。

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

将单元格调暗为范围

    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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    试试这个

    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
    

    来源

    目的地

    【讨论】:

    • 试过了,它不会做任何事情。我还应该注意,“今天”和“明天”再次出现在同一列 (B) 的下方,这就是为什么我试图用我的初始代码(如果你可以这样称呼它)限制搜索范围以不搜索对于“明天”过去的 B100。只是为了检查一下,我从我的工作表中删除了另一个今天和明天,不幸的是它仍然没有做任何事情。感谢您到目前为止的帮助!
    • 如果B 列中的值todaytomorrow 不存在,则不会复制范围
    • 我在一张新纸上试过了,效果很好。不知道为什么它在我的原始表格上不起作用,但我相信我能弄清楚。有没有办法给它添加一个搜索范围,以便我可以将它设置为搜索下一对今天和明天?
    • 是的,但这将是另一种变体,我会在更新中向您展示
    • 是的,今天和明天的第一个效果很好。但是,如果您在下载表的同一列中添加另一个今天和明天,它会将整个内容拉到最后一个明天上方的一个单元格:/
    猜你喜欢
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2016-04-16
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多