VBA to copy data if multiple criteria are met

前端 未结 4 475
鱼传尺愫
鱼传尺愫 2020-12-06 21:18

I am trying to create a VBA code which copies into Sheet \"Results\" the data in the third column of the below tab when the criteria \"Lukas\" in the first column and \"Appl

4条回答
  •  生来不讨喜
    2020-12-06 22:05

    Don't call your sub procedure Copy(). Call it anything else.

    Choose a different destination or you are just going to overwrite the values you are transferring across.

    Sub copyLukasAndApple()
    
        Dim LastRow As Long, i As Long, ws2 as worksheet
    
        with Worksheets("Sheet1")
            LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    
            For i = 2 To LastRow
    
                If .Cells(i, 1) = "Lukas" And .Cells(i, 2) = “Apple” Then
                    with workSheets("Sheet2")
                        .cells(.rows.count, "A").end(xlup).offset(1, 0) = _
                             Worksheets("Sheet1").Cells(i, 3).value
                    end with
                End If
    
            Next i
        end with
    
    End Sub
    

提交回复
热议问题