I’m trying to copy the value ‘10’ into column ‘L’, but it only works when I type it in manually. When I attempt to copy and paste, it doesn’t work.

suraj sharma 85 Reputation points
2023-11-02T05:28:52.3433333+00:00
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim ws As Worksheet
    Dim rng As Range
    Dim temp As Variant

    ' Set your worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    ' Check if the change is made in column L and the cell is not empty
    If Target.Column = 12 And Not IsEmpty(Target) Then

        ' Find matching value in column L
        Set rng = ws.Columns("L").Find(Target.Value, LookIn:=xlValues, lookat:=xlWhole)

        ' If a match is found and it's not the same cell where the change was made
        If Not rng Is Nothing And rng.Address <> Target.Address Then

            ' Ignore the change and exit the subroutine
            Exit Sub

        Else

            ' Find matching value in column E
            Set rng = ws.Columns("E").Find(Target.Value)

            ' If a match is found
            If Not rng Is Nothing Then

                ' Store the row from column A to H of the matching row in a temporary variable
                temp = ws.Range("A" & rng.Row & ":H" & rng.Row).Value

                ' Copy the row from column A to H to the matching row in column L
                ws.Range("A" & rng.Row & ":H" & rng.Row).Value = ws.Range("A" & Target.Row & ":H" & Target.Row).Value

                ' Move the stored row to the original location of the typed row
                ws.Range("A" & Target.Row & ":H" & Target.Row).Value = temp

            End If

        End If
.Row & ":H" & Target.Row).Value = temp

            EndPrivate Sub Worksheet_Change(ByVal Target As Range)
Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
4,224 questions
Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,658 questions
Windows 365 Business
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,687 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,685 questions
{count} votes