簡體   English   中英

根據不同的列將行從一個工作表復制到另一個工作表

[英]Copy rows from one worksheet to another based on different columns

我有一個主表(“ Perk”),並希望將A列中帶有“ y”的所有內容都復制到“ Registration”表中,並將B列中帶有“ y”的所有內容都復制到“ Housing”表中。 使用當前代碼,正確的信息將被復制到注冊表中。 房屋表僅拉入第一條記錄。

Sub extractdata()
    Dim x As Long, lastrow

    lastrow = Sheets("Perk").Range("A" & Rows.Count).End(xlUp).Row

    Sheets("Housing").Range("A2:AW500").ClearContents
    Sheets("Registration").Range("A2:AW500").ClearContents
    For x = 2 To lastrow
    If Worksheets("Perk").Cells(x, 1) Like "y*" Or Worksheets("Perk").Cells(x, 1) Like "Y*" Then
    Worksheets("Perk").Cells(x, "A").EntireRow.Copy Destination:=Sheets("Registration").Range("A" & Rows.Count).End(xlUp).Offset(1)
    End If

    If Worksheets("Perk").Cells(x, 2) Like "y*" Or Worksheets("Perk").Cells(x, 2) Like "Y*" Then
    Worksheets("Perk").Cells(x, "B").EntireRow.Copy Destination:=Sheets("Housing").Range("B" & Rows.Count).End(xlUp).Offset(1, -1)
    End If
    Next x
End Sub

將此視為您的津貼表,

在此處輸入圖片說明

作為登記表的輸出

在此處輸入圖片說明

這是房屋表的輸出

在此處輸入圖片說明 這里發生的事情是陳述的結果

Worksheets("Perk").Cells(x, "A").EntireRow.Copy Destination:=Sheets("Housing").Range("A" & Rows.Count).End(xlUp).Offset(1)

您正在通過檢查A列中的空單元格來查找住房單的空行,這是錯誤的。

還沒有嘗試過,但是如果您更改此行,它應該可以工作

 Worksheets("Perk").Cells(x, "A").EntireRow.Copy Destination:=Sheets("Housing").Range("B" & Rows.Count).End(xlUp).Offset(1, -1)

暫無
暫無

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

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