+ Reply to Thread
Results 1 to 5 of 5

I need help!

Hybrid View

  1. #1
    Registered User
    Join Date
    06-19-2012
    Location
    Oklahoma, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    I need help!

    I am trying to use a paste special that works when an input in my spreadsheet is changed, but for some reason I get into an iterative loop and then crash excel. Can someone tell me why this code doesn't work? And possibly offer suggestions.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim KeyCells As Range
    
        Set KeyCells = Range("c7:c17")
        
        If Not Application.Intersect(KeyCells, Range(KeyCells.Address)) _
               Is Nothing Then
            
        Range("H36").Select
        Selection.Copy
        Range("C26").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("F26").Select
        Application.CutCopyMode = False
            
            
            MsgBox ("it worked")
            
        End If
    End Sub
    Last edited by ChrisB2011; 06-19-2012 at 11:43 AM.

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: I need help!

    You need to add:

    Application.EnableEvents = False
    At the beginning and then:

    Application.EnableEvents = True
    At the end.

    What happens is that when you write to this worksheet, you changed it, so it calls the Worksheet_Change again.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: I need help!

    This line:
    If Not Application.Intersect(KeyCells, Range(KeyCells.Address)) _
    Is Nothing Then
    should presumably be
    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
    since it serves no purpose at the moment.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Registered User
    Join Date
    06-19-2012
    Location
    Oklahoma, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: I need help!

    Thanks Steven!

    I've been messing with this sheet for hours now and that worked perfectly.

  5. #5
    Registered User
    Join Date
    06-19-2012
    Location
    Oklahoma, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: I need help!

    Thanks for the help romperstomper! This is only my first thread, but I feel like I'm going to learn a lot on here.

+ Reply to Thread

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