walter while
walter while

Reputation: 107

VBA Copy paste columns in different sheet

I have two sheets – Latency, TP. I need to copy col M from "Latency" and paste it into col D of "TP" only if "Latency" col E has the string “COMPATIBLE” and col O has the string “Pass”.

I have the below code, but it doesn't give any result.

I'm not sure whats wrong with it:

Sub sbMoveData()
Dim lRow As Integer, i As Integer, j As Integer
'Find last roe in Sheet1
 With Worksheets("Latency")
    lRow = .Cells.SpecialCells(xlLastCell).Row
    j = 1
    For i = 1 To lRow
        If UCase(.Range("E" & i)) = "COMPATIBLE" And UCase(.Range("O" & i)) = "Pass" Then
            .Range("M" & i).Copy Destination:=Worksheets("TP").Range("D" & j)
            j = j + 1
        End If
    Next
End With

End Sub

Upvotes: 0

Views: 151

Answers (3)

sn152
sn152

Reputation: 276

Try this

Sub sbMoveData()
Dim lRow As Integer, i As Integer, j As Integer
Dim ws1, ws2 As Worksheet

Set ws1 = ThisWorkbook.Sheets("Latency")
Set ws2 = ThisWorkbook.Sheets("TP")
'Find last roe in Sheet1

lRow = ws1.Cells.SpecialCells(xlLastCell).Row
j = 1
For i = 1 To lRow
    If ws1.Range("A" & i) = "COMPATIBLE" And ws1.Range("B" & i) = "Pass" Then
        ws1.Range("M" & i).Copy Destination:=ws2.Range("D" & j)
        j = j + 1
    End If
Next i

End Sub

Upvotes: 0

Hauffa
Hauffa

Reputation: 11

You are never going to match UCase(Cell) = "Pass", right? You either need to have:

UCase(.Range("O" & i)) = "PASS"

or

.Range("O" & i) = "Pass"

Upvotes: 1

dgorti
dgorti

Reputation: 1240

UCase(.Range("O" & i)) = "Pass" Will always be false :-)

Upvotes: 2

Related Questions