簡體   English   中英

搜索后Excel VBA復制粘貼

[英]Excel VBA Copy Paste After Search

我的宏應該只復制粘貼一個數據塊。

我的問題是,它僅將數據復制到一列中,我不確定如何解決該問題。

Sheets("Auswertung").Select
Daten = Range("L4:R17")
Sheets("Übersicht").Select

With Application
    .DisplayAlerts = True
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With

For S = 2 To Range("nz85").End(xlToLeft).Column
    If Cells(85, S) = Sheets("Auswertung").Range("L3") Then
        Range(Cells(86, S), Cells(98, S)) = Daten
        Exit For
    End If

第85行包含日期。 我想在某個日期復制數據。 Daten包含日期(如果為“ L3”)及其后6天的信息。 如此一周的數據。 我以為我可以將一個星期的數據簡單地放入Daten並在一周的第一天粘貼,以希望它也可以在接下來的6天中粘貼。 問題在於它僅將日期粘貼到一欄中。

我該如何解決?

嘗試這個

Range(Cells(86, S), Cells(98, S+6)) = Daten

重要說明 :您嘗試粘貼到13行Range(Cells(86, S), Cells(98, S))是= 98-86 +1 = 13行 雖然Daten = Range("L4:R17")實際上是14行 因此,您會遇到錯誤,因為RangeDaten數組的大小不匹配。

Column的大小也是如此,您可以將6列復制到1中。(如@ h2so4所述)

因此,要么需要修改您的Daten ,要么是Range(Cells(86, S), Cells(99, S))

同樣,您可以實現您想要的目標,而無需Select所有不必要的不​​同工作表。 只需使用完全合格的RangeCells例如以下代碼:

Daten = Sheets("Auswertung").Range("L4:R17").Value

With Worksheets("Übersicht")
    For s = 2 To .Range("NZ85").End(xlToLeft).Column
        If .Cells(85, s) = Sheets("Auswertung").Range("L3").Value Then
            ' ******* MODIFY ONE OF THE PARAMETERS AT THE LINE BELOW *******
            .Range(.Cells(86, s), .Cells(98, s)).Value = Daten
            Exit For
        End If
    Next s
End With

暫無
暫無

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

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