Consulting

Results 1 to 4 of 4

Thread: VBA event to detect an Autofill Scrolling?

  1. #1
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    2
    Location

    VBA event to detect an Autofill Scrolling?

    Hi,

    I am relatively new working with VBA and I am having some troubles writing a macro. I have a Workbook with two worksheets. In the first one I introduce some data and the second one is some sort of backup worksheet where changes in some columns in the first one are saved.

    I was able to program a script to detect Worksheet_Change so if I rewrite a single cell, the new data are also copied in the backup worksheet. The problem is that oft I need to do Autofill (click right-down and scroll) when I want to use the same formula in many cells and this is not detected by the Worksheet_Change routine. Is there anyway to detect this Autofill event so I can programm the require action?

    Thank you very much

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Welcome to the forum!

    You could probably do it using the Worksheet_SelectionChange instead.
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Newbie
    Joined
    Jan 2018
    Posts
    2
    Location
    Quote Originally Posted by paulked View Post
    Welcome to the forum!

    You could probably do it using the Worksheet_SelectionChange instead.
    The problem with Worksheet_SelectionChange is that it detects every time I select one or more cells. I want the code to be executed only when I do Autofill, no by selecting cells.

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,199
    Location
    How about this:

    Private Sub Worksheet_Change(ByVal Target As Range)    
        Dim r As Range
    
    
        If Target.Address = Cells.Address Then
            MsgBox "Can't select all cells", vbCritical, "Error"
            Application.Undo
            Exit Sub
        End If
        
        If Target.Cells.Count > 1000 Then
            MsgBox "Can't select more than 1000 cells", vbCritical, "Error"
            Application.Undo
            Exit Sub
        End If
        
        For Each r In Target.Cells
            Sheet2.Range(Target.Address).Value = r.Value
        Next r
     
    End Sub
    Hope this helps
    Last edited by georgiboy; 01-24-2018 at 09:58 AM. Reason: Updated code
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

Tags for this Thread

Posting Permissions

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