宏复制到另一张纸上的下一个空白行

Posted

技术标签:

【中文标题】宏复制到另一张纸上的下一个空白行【英文标题】:Macro to copy to next blank row on another sheet 【发布时间】:2016-04-21 11:58:01 【问题描述】:

我正在使用此宏根据一个单元格中的文本从一张纸复制到另一张纸,但每次运行该宏时它都会覆盖数据。有什么方法可以更改宏,使其粘贴的任何数据都在下一个空白行中?

谢谢:)

Sub CopyYes()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet

' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("Main Data")
Set Target = ActiveWorkbook.Worksheets("Cheque Data")

j = 1     ' Start copying to row 1 in target sheet
For Each c In Source.Range("A1:A1000")   ' Do 1000 rows
    If c = "Cheque" Then
       Source.Rows(c.Row).Copy Target.Rows(j).End(xlUp).Offset(1)
       j = j + 1
    End If
Next c

    ' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("Main Data")
Set Target = ActiveWorkbook.Worksheets("Gift Card Data")

j = 1     ' Start copying to row 1 in target sheet
For Each c In Source.Range("A1:A1000")   ' Do 1000 rows
    If c = "Gift Card" Then
       Source.Rows(c.Row).Copy Target.Rows(j).End(xlUp).Offset(1)
       j = j + 1
    End If
Next c

    ' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("Main Data")
Set Target = ActiveWorkbook.Worksheets("Promo Code Data")

j = 1     ' Start copying to row 1 in target sheet
For Each c In Source.Range("A1:A1000")   ' Do 1000 rows
    If c = "Promo Code" Then
       Source.Rows(c.Row).Copy Target.Rows(j).End(xlUp).Offset(1)
       j = j + 1
    End If
Next c

Sheets("Main Data").Range("A2:F200").ClearContents
Sheets("Main Data").Range("J2:Q200").ClearContents

结束子

【问题讨论】:

【参考方案1】:

在每个j=1之前添加

lastrow = Target.Range("A65000").End(xlUp).Row + 1

并将j = 1 更改为j = lastrow

【讨论】:

以上是关于宏复制到另一张纸上的下一个空白行的主要内容,如果未能解决你的问题,请参考以下文章

从一张纸复制数据并将其粘贴到另一张纸上

将每个唯一值从一张纸复制并粘贴到另一张纸上

从范围循环中的每个单元格复制数据并将其粘贴到另一张纸上

宏 - 如果值满足条件复制粘贴到另一张纸

我试图将一张纸上的错误打印在另一张纸上,但无法弄清楚如何

Word怎样在一张纸上放多张图片