簡體   English   中英

VBA將滿足條件的多行復制到另一張工作表

[英]VBA copy mutlipe rows that meet criteria to another sheet

我真的不太了解VBA,所以請耐心等待。

我有一個分配給特定航班(LEGID)的人員列表,我想將這些人員(Worksheet pax)復制到另一個工作表中的特定單元格(temp-單元格b15),但這是行不通的。

該數據表是來自Salesforce的查詢報告。

Sub pax()

   Dim LastRow As Long
   Dim i As Long, j As Long
   Dim legid As String

Application.ScreenUpdating = False

legid = ThisWorkbook.Worksheets("setup").Range("SelReq").Value

Debug.Print legid


   'Find the last used row in a Column: column A in this example
   With Worksheets("pax")
      LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   End With

'   MsgBox (LastRow)
   'first row number where you need to paste values in temp'
   With Worksheets("temp")
      j = .Cells(.Rows.Count, "a").End(xlUp).Row + 1
   End With

   For i = 1 To LastRow
       With Worksheets("pax")
           If .Cells(i, 1).Value = legid Then
               .Rows(i).Copy Destination:=Worksheets("temp").Range("a" & j)
               j = j + 1
           End If
       End With
   Next i

Application.ScreenUpdating = True

End Sub

如果您只想復制名稱。 您可以使用它; 但是,如果您將圖紙名稱和范圍命名為range,則需要更新它們。 此代碼在Sheet3上的某個特定單元格中查找值,然后,如果該值與Sheet1上某個范圍內的值匹配,它將把Sheet1上B列的值放入Sheet2

Sub Test()
    Dim cell As Range
    Dim LastRow As Long, i As Long, j As Long
    Dim legid As String


    With Sheet1
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    With Sheet2
        j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With

    legid = Sheet3.Range("A1")

    For i = 2 To LastRow
        For Each cell In Sheet1.Range("A" & i)
            If cell.Value = legid Then
                Sheet2.Range("A" & j) = cell.Offset(0, 1).Value
                j = j + 1
            End If
        Next cell
    Next i

End Sub

暫無
暫無

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

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