Consulting

Results 1 to 11 of 11

Thread: Worksheet change event help

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Worksheet change event help

    I have a worksheet that when rows are added to it I want them to be copied to another worksheet. So, if sheet("Static") has rows added to the bottom they need to be copied automatically to the next available row in sheet("Summary")

    Code for worksheet change event in the Static sheet:

    Private Sub Worksheet_Change(ByVal Target As Range)
    UpdateFromStatic
    End Sub
    Sub in module:

    Sub UpdateFromStatic()
        
        Dim LR As Long, i As Long
        LR = Range("A" & Rows.Count).End(xlUp).Row
        For i = 2 To LR
        Range("A" & i).Value.Rows(i).Copy Destination:=Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Offset(1)
        
        Next i
    
    
    End Sub
    Currently it doesn't copy anything.
    Peace of mind is found in some of the strangest places.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I don't think you were taking advantage of 'Target' range in the WS event


    Not perfect since it assumes that Col A contains data to determine the last used row, and that you can't add (say) 10 rows which replace the last 5 and add 5 more



    In a standard module

    Option Explicit
     
    Public iLastRowInStatic As Long
    Public wsStatic As Worksheet, wsSummary As Worksheet


    In ThisWorkbook

    Option Explicit
    Private Sub Workbook_Open()
        Set wsStatic = Worksheets("Static")
        Set wsSummary = Worksheets("Summary")
        
        With wsStatic
            iLastRowInStatic = .Cells(.Rows.Count, 1).End(xlUp).Row
        End With
    End Sub


    In WS "Static" module

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        If Target.Rows(1).Row > iLastRowInStatic Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            
            Call Target.EntireRow.Copy(wsSummary.Cells(wsSummary.Rows.Count, 1).End(xlUp).Offset(1, 0).EntireRow)
           iLastRowInStatic = wsStatic.Cells(wsStatic.Rows.Count, 1).End(xlUp).Row
           
            Application.EnableEvents = True
            Application.ScreenUpdating = True
           
        End If
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    I downloaded your workbook and tried to enter data on the Static sheet. Doesn't auto copy to the Summary sheet.
    Peace of mind is found in some of the strangest places.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I think what was happening is that the iLastRowInStatic counter is set when you open the WB, but if you delete rows it doesn't get updated

    Add this to the Workbook Module or try the attachment and see


    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
        With wsStatic
            iLastRowInStatic = .Cells(.Rows.Count, 1).End(xlUp).Row
        End With
    End Sub
    
    
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        With wsStatic
            iLastRowInStatic = .Cells(.Rows.Count, 1).End(xlUp).Row
        End With
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    That works fine. Thanks.
    Peace of mind is found in some of the strangest places.

  6. #6
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Spoke too soon. When trying to add a record to the Static Sheet, I get the following error:

    object variable or with block variable not set.

    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        With wsStatic
            iLastRowInStatic = .Cells(.Rows.Count, 1).End(xlUp).Row
        End With
    End Sub
    specifically on this line:

    iLastRowInStatic = .Cells(.Rows.Count, 1).End(xlUp).Row


    BTW, it compiles fine.
    Peace of mind is found in some of the strangest places.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    wsStatic is Set in the WB open event, but some how was 'disconnected' and is now Nothing

    Replace it with Worksheets("Static") as see
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    That fixed that. Now how can you copy just cells A:E from that row of Static to Summary?
    Peace of mind is found in some of the strangest places.

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Probably something like this


    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rColumnsAtoE As Range
        If Target.Rows(1).Row > iLastRowInStatic Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            
            Set rColumnsAtoE = Intersect(Target, Me.Columns("A:E"))
            
            Call rColumnsAtoE.Copy(Worksheets("Summary").Cells(Worksheets("Summary").Rows.Count, 1).End(xlUp).Offset(1, 0))
           iLastRowInStatic = Worksheets("Static").Cells(Worksheets("Static").Rows.Count, 1).End(xlUp).Row
           
            Application.EnableEvents = True
            Application.ScreenUpdating = True
           
        End If
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    That works almost. Say to are pasting the cells in the Static sheet on row 59. It copies the cells on the summary to row 61 so I have 2 blank rows.
    Peace of mind is found in some of the strangest places.

  11. #11
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    never mind user error. Thanks.

Posting Permissions

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