2

Hopefully this question hasn't already been asked, I tried searching for an answer and couldn't find anything.

This is probably a simple question, but I am writing my first macro in excel and am having a problem that I can't find out a solution to. I wrote a couple of macros that basically sum up columns dynamically (so that the number of rows can change and the formula moves down automatically) based on a value in another column of the same row, and I call those macros from the event Workbook_SheetChange.

The problem I'm having is, I change a cell's value from my macro to display the result of the sum, and this then calls Workbook_SheetChange again, which I do not want. Right now it works, but I can trace it and see that Workbook_SheetChange is being called multiple times. This is preventing me from adding other cell changes to the macros, because then it results in an infinite loop.

I want the macros to run every time a change is made to the sheet, but I don't see any way around allowing the macros to change a cell's value, so I don't know what to do. I will paste my code below, in case it is helpful.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim Row As Long
    Dim Col As Long
    Row = Target.Row
    Col = Target.Column
    If Col <> 7 Then
        Range("G" & Row).Select
        Selection.Formula = "=IF(F" & Row & "=""Win"",E" & Row & ",IF(F" & Row & "=""Loss"",-D" & Row & ",0))"
        Target.Select
    End If
    Call SumRiskColumn
End Sub

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Call SumOutcomeColumn
End Sub

Sub SumOutcomeColumn()
    Dim N As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    Cells(N + 1, "G").Formula = "=SUM(G2:G" & N & ")"
End Sub

Sub SumRiskColumn()
    Dim N As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    Dim CurrTotalRisk As Long
    CurrTotalRisk = 0
    For i = 2 To N
        If IsEmpty(ActiveSheet.Cells(i, 6)) And Not IsEmpty(ActiveSheet.Cells(i, 1)) And Not IsEmpty(ActiveSheet.Cells(i, 2)) And Not IsEmpty(ActiveSheet.Cells(i, 3)) Then
            CurrTotalRisk = CurrTotalRisk + ActiveSheet.Cells(i, 4).Value
        End If
    Next i
    Cells(N + 1, "D").Value = CurrTotalRisk
End Sub

Thank you for any help you can give me! I really appreciate it.

pnuts
  • 58,317
  • 11
  • 87
  • 139
user2947014
  • 81
  • 1
  • 6
  • `Hopefully this question hasn't already been asked, I tried searching for an answer and couldn't find anything.` Yes (a variant of this) has been asked before. See [THIS](http://stackoverflow.com/questions/13860894/ms-excel-crashes-when-vba-code-runs) – Siddharth Rout Nov 03 '13 at 05:39

1 Answers1

5

Use Application.EnableEvents to prevent Excel from calling event procedures.

Put Application.ScreenUpdating = False at the beginning of code and Application.ScreenUpdating = True at the end.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Application.EnableEvents = False
    Dim Row As Long
    Dim Col As Long
    Row = Target.Row
    Col = Target.Column
    If Col <> 7 Then
        Range("G" & Row).Select
        Selection.Formula = "=IF(F" & Row & "=""Win"",E" & Row & ",IF(F" & Row & "=""Loss"",-D" & Row & ",0))"
        Target.Select
    End If
    Call SumRiskColumn
    Application.EnableEvents = True
End Sub

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
    Application.EnableEvents = False
    Call SumOutcomeColumn
     Application.EnableEvents = True
End Sub
Santosh
  • 12,175
  • 4
  • 41
  • 72
  • 1
    Thank you so much! This is _exactly_ what I was looking for. I thought there was probably something I could set like this, but I just didn't know how to search for it. Thanks again! – user2947014 Nov 02 '13 at 07:37