2017-10-05 70 views
1

我測試了一個查找字符串「MISSING」並將數據複製/粘貼到另一個表單的小腳本。如何僅複製特定單元格

Dim c As Range 
Dim j As Integer 
Dim Source As Worksheet 
Dim Target As Worksheet 
Dim sht As Worksheet 
Dim LastRow As Long 

Set sht = ThisWorkbook.Worksheets("Impact Analysis") 
LastRow = sht.Cells(sht.Rows.count, "A").End(xlUp).Row 
Set Source = ActiveWorkbook.Worksheets("Impact Analysis") 
Set Target = ActiveWorkbook.Worksheets("MISSING") 

    j = 11  ' Start copying to row 11 in target sheet 
    For Each c In Source.Range("O11:O" & LastRow) 
     If c = "MISSING" Then 
      Source.Rows(c.Row).Copy Target.Rows(j) 
      j = j + 1 
     End If 
    Next c 

然而,除了應對整個行,我想複製下面的欄目,用相同的相應行,按以下順序:J-,F,E,A和I.所以,從'影響分析'工作表中,如果列O有單詞'MISSING',我想複製J,F,E,A和I以及列O中的數據,並將所有這些粘貼到名爲'MISSING' 」。我如何修改代碼來做到這一點?

回答

1

使用當前的代碼,你可以試試下面:從列

For Each c In Source.Range("O11:O" & LastRow) 
    If UCase(c.Value2) = "MISSING" Then ' non case sensitive search 
     c.Offset(, -5).Copy Target.Range("A" & j) 'copy J 
     c.Offset(, -9).Copy Target.Range("B" & j) 'copy F 
     c.Offset(, -10).Copy Target.Range("C" & j) 'copy E 
     c.Offset(, -14).Copy Target.Range("D" & j) 'copy A 
     c.Offset(, -6).Copy Target.Range("E" & j) 'copy I 
     c.Copy Target.Range("F" & j) 'copy O 

     j = j + 1 
    End If 
Next 

以上副本值的順序指定並把它放在列A至F的目標板材。

相關問題