Results 1 to 7 of 7

Sub won't work on change event

Threaded View

  1. #1
    Valued Forum Contributor
    Join Date
    03-23-2008
    Location
    Tennessee
    MS-Off Ver
    Office 2007
    Posts
    706

    Sub won't work on change event

    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.
    Last edited by hutch@edge.net; 03-10-2010 at 02:11 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1