簡體   English   中英

復制將行從一張紙粘貼到另一張紙

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

我正在嘗試從Sheet1復制滿足條件的行,並將整行發布在當前數據的末尾。 我可以復制該行,但不能粘貼它。 幫助將不勝感激。 這是我寫的代碼:

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

我們走了:縮短了一點,但是應該做...

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

為了提供更多幫助,為什么當您一次只能過濾和復制所有數據時,為什么要花所有(處理)時間遍歷一個可能很大的行集呢?

請參見下面的代碼。 您可能需要對其進行一些調整以匹配您的數據集。

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

//只需使用它。

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

Sheet2.Paste (Rows(index))

如果要復制,請粘貼兩行或更多行,然后使用for循環。

暫無
暫無

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

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