2017-01-24 14 views
0

Excel 2016 - ユーザーが列Dに "CRED"と入力すると、行全体をコピーして次の行に挿入する必要があります。VBA - 行を挿入する値としてコピー&ペーストの式

次のコードは、RECMER(列D)の新しいコードを含む次の行AOKを挿入します。これに加えて、式(列C)を次の行の同じ列の値としてコピー/貼り付けする必要があります。

Private Sub Worksheet_Change(ByVal Target As Range) 

On Error GoTo errHnd 

'Don't do anything if more than one cell has been changed 
If Target.Cells.Count > 1 Then Exit Sub 

'Determine if the changed cell is in Column C and is a Y 
If Target.Column = 4 Then 
    If Target = "CRED" Then 
     'Disable events so code doesn't fire again when row is inserted 
     Application.EnableEvents = False 

     'Copy & Insert changed Row, Clear dotted lines 
     Target.EntireRow.Copy 
     Range("A" & Target.Row + 1).Insert Shift:=xlDown 
     Application.CutCopyMode = False 

     'Put 2201 in Column B of inserted Row 
     Range("D" & Target.Row + 1) = "RECMER" 
    End If 
End If 

errHnd: 
'Re-enable event 
Application.EnableEvents = True 

End Sub 

答えて

0

以下のコード試してみてください:素晴らしい

Private Sub Worksheet_Change(ByVal Target As Range) 

On Error GoTo errHnd 

'Don't do anything if more than one cell has been changed 
If Target.Cells.Count > 1 Then Exit Sub 

'Determine if the changed cell is in Column C and is a Y 
If Target.Column = 4 Then 
    If Target = "CRED" Then 
     'Disable events so code doesn't fire again when row is inserted 
     Application.EnableEvents = False 

     'Copy & Insert changed Row, Clear dotted lines 
     Target.EntireRow.Copy 
     Range("A" & Target.Row + 1).Insert Shift:=xlDown 
     Application.CutCopyMode = False 

     'Put 2201 in Column B of inserted Row 
     Range("D" & Target.Row + 1) = "RECMER" 

     ' ======= Added the 2 lines of code below ======= 
     ' copy >> paste special values only from Column C 
     Range("C" & Target.Row).Copy 
     Range("C" & Target.Row + 1).PasteSpecial xlPasteValues 
    End If 
End If 

errHnd: 
'Re-enable event 
Application.EnableEvents = True 

End Sub 
+0

- 非常に感謝を! – Steph

関連する問題