Modification to VBA Code - Copying data after drop down list value change and pasting to another sheet

Alverna 0 Reputation points
2025-07-02T10:04:23.8833333+00:00

Hi. I have this code which I need some assistance modifying - see below.

  1. First modification is the DropDownCell range should include all of column A that contains data not just cell A1. So if you change the value of any cell in column A using the drop down list the code will copy that row and paste in the destination sheet. For example you change the value in cell A7 using the drop down list. The code then copies row 7.
  2. At the moment the code selects the whole of the row to copy and then paste but it only needs to copy from column B to column D and paste that to the destination sheet. Using the example above the code would copy B7:D7 and paste this into the destination sheet.

Any help would be much appreciated. Very beginner here. Thanks.

Private Sub Worksheet_Change(ByVal Target As Range)
  
   Dim SourceSheet As Worksheet, DestSheet As Worksheet
   Dim SourceRange As Range, DestRange As Range
   Dim DropDownCell As Range
   Dim LastRow As Long, i As Long
   Dim CopyRow As Long

   ' Set the sheet and cell references

   Set SourceSheet = ThisWorkbook.Sheets("DataSheet")
   Set DestSheet = ThisWorkbook.Sheets("DestinationSheet")
   Set DropDownCell = Me.Range("A1") ' Change "A1" to the cell containing the dropdown list
  
   ' Check if the changed cell is the dropdown cell
   If Not Intersect(Target, DropDownCell) Is Nothing Then
       ' Find the last row with data in the source sheet
       LastRow = SourceSheet.Cells(Rows.Count, "A").End(xlUp).Row

       ' Find the row to copy based on the dropdown selection
       For i = 2 To LastRow ' Assuming data starts from row 2
           If SourceSheet.Cells(i, 1).Value = DropDownCell.Value Then 
       ' Assuming the dropdown value matches the first column in the     
         data sheet
               CopyRow = i
               Exit For
           End If
       Next i

       ' If a matching row is found, copy the row
       If CopyRow > 0 Then
           ' Set the range to copy from the source sheet
           Set SourceRange = SourceSheet.Rows(CopyRow)
           ' Find the next available row in the destination sheet
           LastRow = DestSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
           ' Set the range to paste to the destination sheet
           Set DestRange = DestSheet.Rows(LastRow)
           ' Copy the row
           SourceRange.Copy
           DestRange.PasteSpecial xlPasteValues
           Application.CutCopyMode = False 'Clear the copy/paste buffer
           ' Optionally, clear the source row after copying
           'SourceRange.ClearContents
       End If
   End If
End Sub
Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Barry Schwarz 3,836 Reputation points
    2025-07-02T18:43:27.64+00:00

    You can set DropDownCell to Me.Range("A:A")

    Since this will also include cells that don't have values, the test for a valid intersection needs to account for this.

    • Define a new range.
    • Set this range to the return from Intersect.
    • If this range is not Nothing, then verify it has a value
    • Once the intersection is known to be valid, proceed with finding LastRow and the rest of your code.

    Something along the lines of

    Dim Sect as Range
    ...
    Set Sect = Intersect(Target, DropDownCell)
    If Not Sect Is Nothing Then
        If Sect.Value <> "" Then
            (your code)
        End I
    End If
    

    Change the assignment of SourceRange to

    Set SourceRange = Range(SourceSheet.Cells(CopyRow,2),SourceSheet.Cells(CopyRow,4))
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.