简体   繁体   中英

Automatically Copy Cells to Another Worksheet Based on Specific Words

When the word "Overdue" appears in a cell (in column H), I want the name (in column A) and the date (in column F) in that row to automatically copy and paste into another worksheet (named HomePage) and appear in column C12 and E12.

I have the following code, but it's cutting and pasting the entire row. I just want a copy and paste of the name and date to my HomePage.

Private Sub Worksheet_Change (ByVal Target As Range)
  If Target.Column = 8 Then
    If Target.Value = "Overdue" Then
      R = Target.Row
      Rows(R).Cut
      Worksheets("HomePage").Select
      With ActiveSheet
         lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
         .Cells(lastrow, 1).Select
         .Paste
      End With
    End If
  End If
End Sub

Since you only want two cells, why copy and paste? Just assign the values directly.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim R As Long
    If Target.Column = 8 Then
        If Target.Value = "Overdue" Then
            With Sheets("Homepage")
                R = .Cells(.Rows.Count, 3).End(xlUp).Row + 1
                .Cells(R, 3) = Target.Offset(0, -7) ' Column C = Column A
                .Cells(R, 5) = Target.Offset(0, -2) ' Column E = Column F
            End With
        End If
    End If
End Sub

as per your narrative

I want the name (in column A) and the date (in column F) in that row to automatically copy and paste into another worksheet (named HomePage) and appear in column C12 and E12.

place the following code in your worksheet code pane

Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If .Column = 8 And .Value = "Overdue" Then
            Sheets("Homepage").Range("C12").Value = Range(.row, "A")
            Sheets("Homepage").Range("E12").Value = Range(.row, "F")
        End If
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM