2014-11-25 43 views
0

我有這個公式來生成sheet4的結果。但是,我只希望將來自sheet3的列A的匹配條件複製到sheet4,而不是整個行。excel VBA複製粘貼只有列A行?

在這種情況下

,我只想36238和63545在sheet4作爲輸出

一直試圖修改夫婦,不能工作來展示,所以只粘貼原來的代碼在這裏看到,如果任何人都可以針點的修改。

這裏是我的全碼:

Private Sub CommandButton1_Click() 

    FilterCriteria1 

End Sub 

Sub FilterCriteria1() 

Dim LSearchRow As Long 
Dim shtSearch As Worksheet 
Dim shtCopyTo As Worksheet 
Dim rw As Range 

    LSearchRow = 2 'Start search in row 2 

    Set shtSearch = Sheets("Sheet3") 
    Set shtCopyTo = Sheets("Sheet4") 

On Error GoTo Err_Execute 

    Do While Len(shtSearch.Cells(LSearchRow, 1).Value) > 0 

     Set rw = shtSearch.Rows(LSearchRow) 

     If rw.Cells(4).Value = 0 And rw.Cells(7).Value <= -0.4 Then 

      MsgBox "bingo: " & rw.Cells(4).Value & "_" & rw.Cells(7).Value 

      rw.Copy shtCopyTo.Cells(Rows.count, 1).End(xlUp).Offset(1, 0) 

     End If 
     LSearchRow = LSearchRow + 1 
    Loop 

Err_Execute: 
    If Err.Number = 0 Then MsgBox "All have been copied!" Else _ 
    MsgBox Err.Description 


End Sub 

sheet3 and sheet4 output

回答

0

它看起來是您的線路:

rw.Copy shtCopyTo.Cells(Rows.count, 1).End(xlUp).Offset(1, 0) 

我想你可能想:

Cells(rw, 1).copy shtCopyTo.Cells(Rows.count, 1).End(xlUp).Offset(1, 0) 
+0

是的,當然這是李的問題嗯:)我試過你的,但它出來錯誤說類型不匹配 – 2014-11-25 03:45:15

+0

也許將該行上的變量替換爲LSearchRow而不是rw – 2014-11-25 03:52:13