Stop work change event to trigger on other workbooks

mrajesh123

New Member
Joined
Dec 27, 2013
Messages
1
Hi All,

I'm struggling to find answer for a simple problem. I have a code that runs a macro if a user copies and pastes data onto any sheet of a workbook (lest call it - "workbook A"). The issue is, this macro runs on other workbooks when i copy paste any information. How can i prevent the macro triggering on other workbooks?

More detailed explanation:
When i copy and paste something into a new workbook (lets call it - "Book1"), excel opens the workbook name that has the macro and looks for a sheet which obviously it can't find, therefore displays Run-time error 9: Subscript out of range


Please HELP!!

ThisWorkBook Code
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If Not Intersect(Target, Range("$A$2:$A$10000")) Is Nothing Then
Application.OnKey "^v", "ActiveCellPaste"
Else
Application.OnKey "^v"
End If
End Sub




Module 1 Code

Sub ActiveCellPaste()


Dim lookFor As Range
Dim rng As Range
Dim col As Integer
Dim found As Variant
Dim x As Integer


ActiveSheet.Paste


Set lookFor = ActiveCell.Offset(0, 0)
Set rng = Sheets("2monthdata").Columns("A:B") 'this is where macro breaks and gives error message'
col = 1
'Get last Row
x = Range(Range("A1"), Range("A100000").End(xlUp)).Count
Application.EnableEvents = False
On Error Resume Next
found = Application.VLookup(lookFor.Value, rng, col, 0)
If IsError(found) Then
Range("B" & x).Value = "New"
Range("C" & x).Value = Date
Range("D" & x).Value = UserName()
Else
Range("B" & x).Value = "Repeat"
Range("C" & x).Value = Date
Range("D" & x).Value = UserName()


End If


If ActiveCell.Offset(-1, 1).Value = "New" Then
ActiveCell.Offset(-1, 4).Select
Else
ActiveCell.Offset(0, 0).Select
End If
Application.EnableEvents = True
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
you appear to be mixing up the terms "workbook" and "worksheet"

i think that you meant to say that you want to prevent code from triggering on other worksheets

simply check worksheet name and proceed accordingly

Code:
' ThisWorkBook Code

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Sh.Name <> "[COLOR=#574123]worksheet A" then exit sub   ' exits if this is now sheet("workbook A")[/COLOR]

    If Not Intersect(Target, Range("$A$2:$A$10000")) Is Nothing Then
        Application.OnKey "^v", "ActiveCellPaste"
    Else
        Application.OnKey "^v"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,008
Messages
6,128,256
Members
449,435
Latest member
Jahmia0616

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top