Hello,
I put together a little macro attempting to total up some values based on what was located in one array and then write the new values (totals) to another part of the spreadsheet. It may not be the prettiest of code, but I believe everything works except I am unable to successfully write the final array to the spreadsheet. In fact, nothing is written and I'm at a loss as to why. If someone could take a look at the following code and identify what I'm missing or doing wrong, I would very much appreciate it.
I put together a little macro attempting to total up some values based on what was located in one array and then write the new values (totals) to another part of the spreadsheet. It may not be the prettiest of code, but I believe everything works except I am unable to successfully write the final array to the spreadsheet. In fact, nothing is written and I'm at a loss as to why. If someone could take a look at the following code and identify what I'm missing or doing wrong, I would very much appreciate it.
VBA Code:
Sub Total2()
Dim array_MaxPlay As Variant
Dim LastRow As Integer
Dim Max1 As Integer
Dim Max2 As Integer
Dim Max3 As Integer
Dim MTotal1 As Integer
Dim MTotal2 As Integer
Dim MTotal3 As Integer
Dim MTotal4 As Integer
Sheets("Courts1").Select ' To populate next 3 variables; only need to read each just once
Max1 = Range("S3").Value
Max2 = Range("S4").Value
Max3 = Range("U4").Value
MTotal1 = 0
MTotal2 = 0
MTotal3 = 0
MTotal4 = 0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' C7:04851 is the MAX size of the array. The following is used to identify last row actually being used
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
LastRow = Sheets("Courts1").Range("C7:O4851").Find(What:="*", After:=Sheets("Courts1").Range("C7:O4851").Cells(1, 1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Based on the information above, able to populate array_MaxPlay
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
array_MaxPlay = Range("C7:O" & LastRow).Value
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Counter2 is for Row location. In this example, LastRow = 76, minus 6 should be a constant and that brings us to 70; the actual range of the array
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For Counter2 = 1 To (LastRow - 6)
For Counter1 = 8 To 13 'Columns; 8 - 13 are the locations of the values being examined and tallied
If array_MaxPlay(Counter2, Counter1) = Max1 Then
MTotal1 = MTotal1 + 1
ElseIf array_MaxPlay(Counter2, Counter1) = Max2 Then
MTotal2 = MTotal2 + 1
ElseIf array_MaxPlay(Counter2, Counter1) = Max3 Then
MTotal3 = MTotal3 + 1
Else: MTotal4 = MTotal4 + 1
End If
Next Counter1
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Once all columns in a row are examined, store results in the appropriate location in an array called array_MaxPlayT
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ReDim array_MaxPlayT(1 To LastRow, 1 To 4) As Variant
array_MaxPlayT(Counter2, 1) = MTotal1
array_MaxPlayT(Counter2, 2) = MTotal2
array_MaxPlayT(Counter2, 3) = MTotal3
array_MaxPlayT(Counter2, 4) = MTotal4
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Reset the following variables back to ZERO in preparation of examining the next row in the array called array_MaxPlay (not array_MaxPlayT)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
MTotal1 = 0
MTotal2 = 0
MTotal3 = 0
MTotal4 = 0
Next Counter2
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Once all rows are examined and tallied, write results in array_MaxPlayT to the following range
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sheets("Courts1").Range("R7:U" & LastRow) = array_MaxPlayT
End Sub