2

I am having trouble running a macro that copies data range (source) from one worksheet to the main worksheet (destination) based on a value selected in a cell in the destination worksheet. The copy sub works fine when ran separately, but when I try to call the sub based on a worksheet sub the copy paste fails. I know I am probably missing something very simple but can't quite get it. Here are the two code sets:

Worksheet Sub:

Sub worksheet_change(ByVal target As Range)
Set target = Range("D2")
If target.Value = "BEO" Then
Call BEO
End If
End Sub

Module Sub:

Sub BEO()
 
    Sheets("data").Select
    ActiveSheet.Range("N24:P25").Select
    Selection.Copy
 
    Sheets("Configuration").Select
    ActiveSheet.Range("E1").Select
    Selection.PasteSpecial Paste:=xlPasteAll
  
    Application.CutCopyMode = False

End Sub

I am trying to initiate Module sub to run based on value in cell.

Mayukh Bhattacharya
  • 12,541
  • 5
  • 21
  • 32
jchrist
  • 23
  • 3

1 Answers1

1

The variable Target is SET automatically once the worksheet_change() subroutine is triggered. It will be set with the range that triggered the subroutine to run. So instead of setting it yourself, you want to test what range it holds to see if it's the cell you are interested in, and if so, if it has the value you are looking for.

Sub worksheet_change(ByVal target As Range)
    'Test if Target (the range that triggered this to run) intersects with cell D2
    If Not Intersect(Target, Range("D2")) Is Nothing Then
        If Range("D2").Value = "BEO" Then
            Call BEO
        End If
    End If
End Sub
JNevill
  • 46,980
  • 4
  • 38
  • 63