VBA Excel - Workbook_SheetChange
- by user2947014
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.