简体   繁体   English

复制将行从一张纸粘贴到另一张纸

[英]Copy paste a row from one sheet to another

I am trying to copy rows from Sheet1 which meet a crieteria and post the whole row at the end of the current data. 我正在尝试从Sheet1复制满足条件的行,并将整行发布在当前数据的末尾。 I am able to copy the row but it is not pasting it. 我可以复制该行,但不能粘贴它。 Help will be appreciated. 帮助将不胜感激。 Here is my code I have written: 这是我写的代码:

Sub Button1_Click()

Dim i As Integer

'Range("H2:O65536").ClearContents

Sheets("Sheet1").Select
LastRowColA = Range("A65536").End(xlUp).Row

For i = 2 To LastRowColA

If Cells(i, 6) = "No" Then
Rows(i).Select
Rows(i).Copy

Sheets("Sheet2").Select
Dim LastRow As Long
Dim StartRow As Long
Dim Col As Long
Dim Row As Long

StartRow = 2
Col = 1
LastRow = findLastRow(1)

For Row = StartRow To LastRow
Rows(LastRow).Select
ActiveSheet.Paste

Next Row

Else
'do nothing
End If
Next i
End Sub


Function findLastRow(ByVal Col As Integer) As Long
    'Find the last row with data in a given column

    findLastRow = Cells(Rows.Count, Col).End(xlUp).Row
End Function

here we go: a tad shorter, but should do the job... 我们走了:缩短了一点,但是应该做...

Sub Button1_Click()
Dim i As Integer
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")

For i = 2 To ws1.Range("A65536").End(xlUp).Row
    If ws1.Cells(i, 6) = "No" Then ws1.Rows(i).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, 6).End(xlUp).Row + 1)
Next i
End Sub

To add a bit more help, why spend all that (processing) time looping through a potentially large row set when you can just filter and copy all your data at once? 为了提供更多帮助,为什么当您一次只能过滤和复制所有数据时,为什么要花所有(处理)时间遍历一个可能很大的行集呢?

See code below. 请参见下面的代码。 You may need to tweak it a bit to match your data set. 您可能需要对其进行一些调整以匹配您的数据集。

Sub Button1_Click()

Dim ws1 as Worksheet: Set ws1 = ThisWorkbook.Sheets("Sheet1")
Dim ws2 as Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")

With ws1

    .UsedRange.AutoFilter 6, "No" 
    '-> assumes data starts in column A, if not adjust the 6

    Intersect(.UsedRange,.UsedRange(Offset(1)).SpecialCells(xlCellTypeVisible).Copy  
    ' -> assumes No's are there, if they may not exist, will need to error trap.

End With

With ws2

    .Rows(.Cells(ws2.Rows.Count, 6).End(xlUp).Row + 1).PasteSpecial xlPasteValues

End With

ws1.AutoFilterMode = False

End Sub

// Just use it. //只需使用它。

Sheet2.Select (Sheet1.Rows(index).Copy)

Sheet2.Paste (Rows(index))

If you want to copy, paste two or more rows then use the for loop. 如果要复制,请粘贴两行或更多行,然后使用for循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA从自动筛选器中复制并粘贴到另一张工作表中,输出一行 - VBA Copy and Paste in another Sheet from AutoFilter outputting one row 将整行从一张纸复制并粘贴到另一张纸上 - copy and paste entire row from one to another sheet issue 使用VBA excel从一张工作表中复制表格并将其粘贴到另一张工作表的下一个空行中 - copy a table from one Sheet and paste it in the next empty row of another Sheet using VBA excel 如何从一个工作表中复制特定的列并粘贴到第一行中不同范围的另一工作表中? - How to copy specific columns from one sheet and paste in another sheet in a different range at the first row? 使用宏从一个工作表中复制多个范围,然后将其粘贴到第一行中的另一工作表中 - Using macro to copy multiple ranges from one sheet, and paste them into another sheet in the first empty row Excel 2007 VBA:如何从一张纸上的动态范围复制和粘贴到另一张纸的第一行? - Excel 2007 VBA: How do I copy and paste from a dynamic range on one sheet to the first empty row of another sheet? 一次将一个单元格从列表复制并粘贴到另一张工作表 - Copy and Paste one Cell at a time from a list to another sheet 使用粘贴在某些条件下将单元格值从一张纸复制到另一张纸 - Copy Cell values from One Sheet to Another on certain criteria with the paste Excel可以将某些单元格从一张纸复制/粘贴到另一张纸上,但要稍加改动 - Excel to copy / paste some cells from one sheet to another but with a twist VBA:从一张纸到另一张纸的多次复制和粘贴 - VBA: multiple copy and paste from one sheet to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM