Alan,
Made some changes to the code. It now clears the L column and puts in the 1's in the L column where appropriate. It uses the arrays in column A to check if a win actually happened or not. Let me know if it is now returning the correct results, or if it still needs tweaking.
Sub tgr()
Dim rngDate As Range
Dim rngVis As Range
Dim VisGrp As Range
Dim DateVal As Double
Dim arrData As Variant
Dim DataIndex As Long
Dim BankStart As Double
Dim BankStop As Double
Dim ProfitThreshold As Double
Dim BankValue As Double
Dim WinAchieved As Boolean
Dim arrResults() As Variant
ReDim arrResults(1 To 5, 1 To Rows.Count)
Dim ResultIndex As Long
Dim rIndex As Long
Dim strYesNone As String
Dim DateFormat As String
'Disable screenupdating, events, and autocalc to allow macro to run faster
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
'Clear column L
Columns("L").ClearContents
'Get the profit threshold % from cell E4
ProfitThreshold = Range("E4").Value2
'This is the range in column B containing the dates
Set rngDate = Range(Cells(1, "B").End(xlDown).Offset(-1), Cells(Rows.Count, "B").End(xlUp))
'Set this to the desired date format
DateFormat = "m/d/yyyy"
With rngDate
'Change the dates to numbers to avoid date formatting issues
.NumberFormat = "General"
'Sort the dates ascending to make processing easier. This line may not be necessary if the data is already sorted
Intersect(.EntireRow, Range("A:K")).Sort Range(.Address), xlAscending, Header:=xlYes
'If a day is skipped, it could cause an error
'This will prevent that and proceed to the next day
On Error Resume Next
'Start a loop going from the first date to the last date
'This loop happens once for each whole day
For DateVal = .Cells(2).Value2 To .Cells(.Cells.Count).Value2
'Filter the data for the current DateVal so that we're working with just that day
.AutoFilter 1, DateVal
'Get the range of visible cells after the filter
Set rngVis = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
'Check if data for this day exists
If Not rngVis Is Nothing Then
If Not rngVis.Offset(, -1).Find("*") Is Nothing Then
'Found data for this day exists, perform operations
'Assign values to variables before looping through the bank data for the day
WinAchieved = False 'Win hasn't been achieved yet (haven't looked yet)
strYesNone = "None" 'This variable declares whether a win was achieved or not for the day
BankStart = Cells(rngVis.Row, "K").Value2 'Gets the bank's starting value for the day
BankStop = BankStart * (1 + ProfitThreshold) 'Gets the stop threshold
'Loop through the bank data for the day, going by the arrays in column A
For Each VisGrp In rngVis.Offset(, -1).SpecialCells(xlCellTypeConstants).Areas
'If the bank data equals or exceeds the bankstop, then a win has been achieved, and exit the loop
If VisGrp.Cells(VisGrp.Cells.Count).Offset(, 10).Value2 >= BankStop Then
WinAchieved = True
strYesNone = "Yes"
'Populate column L with 1's until end of day
Range("L" & VisGrp.Row + VisGrp.Rows.Count & ":L" & rngVis.Row + rngVis.Rows.Count - 1).Value = 1
Calculate
'Load the visible "K" cell values into an array to find exact row and values
arrData = Application.Transpose(Intersect(VisGrp.EntireRow, Columns("K")).Value2)
For DataIndex = LBound(arrData) To UBound(arrData)
If arrData(DataIndex) >= BankStop Then
BankValue = arrData(DataIndex)
rIndex = VisGrp.Row + DataIndex - LBound(arrData)
Exit For
End If
Next DataIndex
Exit For
End If
Next VisGrp
If Not WinAchieved Then
BankValue = rngVis.Cells(rngVis.Cells.Count).Offset(, 9).Value
rIndex = rngVis.Row + rngVis.Rows.Count - 1
End If
'Increases ResultIndex and populates Results Array
ResultIndex = ResultIndex + 1
arrResults(1, ResultIndex) = DateVal 'Date
arrResults(2, ResultIndex) = BankStart 'Bank start value
arrResults(3, ResultIndex) = strYesNone 'Yes or None depending on if a win was achieved
arrResults(4, ResultIndex) = BankValue 'The bank value when the win was achieved or the end of day bank value if a win was not achieved
arrResults(5, ResultIndex) = rIndex 'The row number of the bank value
End If
End If
Next DateVal 'Advance loop to perform these operations on the next day
'Clear On Error Resume Next functionality
On Error GoTo 0
.AutoFilter 'Now that all days have been processed, remove the filter
.NumberFormat = DateFormat 'Change the format back to the DateFormat
End With
'If there are results, output them to the sheet, starting at M36
If ResultIndex > 0 Then
ReDim Preserve arrResults(1 To 5, 1 To ResultIndex)
With Range("M36:Q36").Resize(ResultIndex)
.Value = Application.Transpose(arrResults)
.Resize(, 1).NumberFormat = DateFormat
End With
End If
'Re-enable screenupdating, events, and autocalc
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Bookmarks