Consulting

Results 1 to 3 of 3

Thread: Worksheet_change

  1. #1
    VBAX Regular
    Joined
    Apr 2009
    Posts
    59
    Location

    Worksheet_change

    I have the following in Sheet1:
    [VBA]Private Sub worksheet_change(ByVal Target As Range)
    Call main
    End Sub
    [/VBA]

    And in module 1
    [VBA]
    Sub main()
    ActiveWorkbook.ActiveSheet.Cells(2, 7) = 0
    sumtotal = 0
    For i = 1 To Month(Now)
    row2 = row2 + Row
    Sum = 0
    Row = Application.WorksheetFunction.Match("Total", Range(Cells(row2 + 1, 2), Cells(row2 + 200, 2)), 0)
    If Not IsEmpty(ActiveWorkbook.ActiveSheet.Cells(Row + row2 - 1, 1)) Then
    ActiveWorkbook.ActiveSheet.Cells(Row + row2, 1).EntireRow.Insert
    Row = Row + 1
    End If
    'Summing all the comms for the monthly total
    For j = 1 To Row - 3
    Sum = Sum + ActiveWorkbook.ActiveSheet.Cells(row2 + j + 1, 4)
    Next j
    ActiveWorkbook.ActiveSheet.Cells(Row + row2, 4) = Sum
    sumtotal = sumtotal + Sum
    Next i
    ActiveWorkbook.ActiveSheet.Cells(2, 7) = sumtotal
    End Sub
    [/VBA]

    When I change a cell in the spreadsheet, it seems the program runs forever. When i stop it (by ctrl+break) it gives me a few error messages, and excel even crashes sometimes.

    What is wrong?
    (When i ink the main function to a button it works perfectly)

    Thank you for your help!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try this

    [vba]

    Private Sub worksheet_change(ByVal Target As Range)
    Application.EnableEvents = False
    Call main
    Application.EnableEvents = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Apr 2009
    Posts
    59
    Location
    I'm really impressed by the help one can get here! Thanks so much!!

Posting Permissions

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