簡體   English   中英

Excel VBA在工作表中查找文本,復制范圍,粘貼到其他工作表

[英]Excel vba find text in sheet, copy range, paste to other sheet

我對excel宏非常陌生,無法弄清楚如何修復所需的內容。 基本上,我正在編寫一個代碼,以在工作表中查找文本,當前工作表位於B63(“今天”),從找到的值中選擇該行,直到它應在B83(“ Tomorrow”)處停止的下一個值為止這個案例。 問題在於,每次下載新數據時,“今天”和“明天”都會上下移動。

我已經嘗試為此編寫代碼,但沒有取得任何成功,在這一點上,我什至不確定我是否采用了正確的方法。 這是我所擁有的,任何幫助將不勝感激:

暗單元格范圍

    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

嘗試這個

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

UPDATE

嘗試這個

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

資源

在此處輸入圖片說明

目的地

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM