简体   繁体   English

搜索后Excel VBA复制粘贴

[英]Excel VBA Copy Paste After Search

My macro should simply copy paste a block of data. 我的宏应该只复制粘贴一个数据块。

My problem is, that it only copies the data into one column, and I'm not sure how to fix that. 我的问题是,它仅将数据复制到一列中,我不确定如何解决该问题。

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

Row 85 contains dates. 第85行包含日期。 I want to copy the data in a certain date. 我想在某个日期复制数据。 Daten contains the information of the date if "L3" and the next 6 days. Daten包含日期(如果为“ L3”)及其后6天的信息。 So one week of data. 如此一周的数据。 I thought I can simply put the data of one week into Daten and the paste it on the first day of the week in the hope that it will also paste the next 6 days. 我以为我可以将一个星期的数据简单地放入Daten并在一周的第一天粘贴,以希望它也可以在接下来的6天中粘贴。 The problem is that it only pastes in the date in one column. 问题在于它仅将日期粘贴到一栏中。

How could i fix that? 我该如何解决?

尝试这个

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

Important Note : you are trying to Paste to 13 rows Range(Cells(86, S), Cells(98, S)) is = 98 - 86 + 1 = 13 rows . 重要说明 :您尝试粘贴到13行Range(Cells(86, S), Cells(98, S))是= 98-86 +1 = 13行 While Daten = Range("L4:R17") is actually 14 rows . 虽然Daten = Range("L4:R17")实际上是14行 Therefore, you are getting your error, because the size of the Range and your Daten array don't match. 因此,您会遇到错误,因为RangeDaten数组的大小不匹配。

The same goes for the size of your Column , you can copy 6 columns into 1. (as mentioned also by @h2so4) Column的大小也是如此,您可以将6列复制到1中。(如@ h2so4所述)

So either your Daten need to be modified, or maybe it's Range(Cells(86, S), Cells(99, S)) ? 因此,要么需要修改您的Daten ,要么是Range(Cells(86, S), Cells(99, S))

Also, you can achieve what you are trying to without all the unnecessary Select of the different sheets. 同样,您可以实现您想要的目标,而无需Select所有不必要的不​​同工作表。 Just use fully qualified Range and Cells like the code below: 只需使用完全合格的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