Greetings, Gurus.
I have a small macro that filters and copied data to a "Main" sheet. It works beautifully if I run it from a standard module, but when I try to run it from a Worksheet Change event, it always crashes Excel. Here is the code:
Sub Get_Data()
Application.ScreenUpdating = False
Dim tbl As Range
Dim Loc
Sheets("Main").Select
Loc = Range("D2").Value
ActiveSheet.Unprotect
Range("A6:K11").ClearContents
Range("A15:K21").ClearContents
Sheets("MDC sched").Select
Range("A1").AutoFilter Field:=1, Criteria1:=Loc
Set tbl = Range("A1").CurrentRegion
Application.DisplayAlerts = False
tbl.Offset(1, 0).Resize(tbl.Rows.Count, _
tbl.Columns.Count).Copy
Sheets("Main").Select
Range("A6").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("MDC sched").Select
Range("A1").Select
Application.CutCopyMode = False
Application.DisplayAlerts = True
ActiveSheet.AutoFilterMode = False
Sheets("USA").Select
Range("A1").AutoFilter Field:=1, Criteria1:=Loc
Set tbl = Range("A1").CurrentRegion
Application.DisplayAlerts = False
tbl.Offset(1, 0).Resize(tbl.Rows.Count, _
tbl.Columns.Count).Copy
Sheets("Main").Select
Range("A15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("USA").Select
Range("A1").Select
Application.CutCopyMode = False
Application.DisplayAlerts = True
ActiveSheet.AutoFilterMode = False
Sheets("Main").Select
Range("D2").Select
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
False
Application.ScreenUpdating = True
End Sub
Now, I've never run a macro from a worksheet change event, but I read some examples on here, so I right clicked on the "Main" worksheet, and copied my code between these lines:
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Of course, I removed "Sub Get_Data()" from the top, and "End Sub" from the bottom, but as I said, Excel keeps crashing. What am I doing wrong?
Thanks in advance for any help you can offer.
Bookmarks