2017-01-12 94 views
0

我有两个工作表 - 延迟,TP。只有当“延迟”列E具有字符串“COMPATIBLE”且列C具有字符串“通过”时,我需要从“延迟”中复制列M并将其粘贴到“TP”的列D中。VBA在不同的工作表中复制粘贴列

我有下面的代码,但它没有给出任何结果。

我不知道什么不妥:

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 

末次

+0

您的代码测试的cols E和O,但你说你想测试A和B是那是一个错字还是你真的在测试错误的cols? – DeanOC

+0

抱歉,它的错字...它的COl E和Col O来自延迟表。将编辑OP –

+0

在Clumn E中“当你坐下时有字符串”COMPATIBLE“,你的意思是完全匹配吗?或者也可能是”COMPATIBLE和其他东西“? –

回答

0

试试这个

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 
+0

真棒,这是有效的,我如何将它粘贴到D2而不是D1? –

+0

只需将j = 1改为j = 2。 – sn152

2

用Ucase(.Range( “O” & I))= “通过” 永远是假的: - )

+0

我该如何解决这个问题? –

+1

UCase(.Range(”O“ &i))=“PASS” – dgorti

+0

@dgorti编辑你的答案,并把你的修正放在它里面 –

1

你永远不会匹配UCase(Cell)=“Pass”,对吧?你要么需要有:

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

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